This question already has answers here:
Is there any difference if we define friend function inside or outside of class
(6 answers)
Closed 5 years ago.
In C++, we are allowed to define a friend function inside the class definition
like:-
class A {
public:
A(int a): mem(a){}
~A() {}
friend void fun() {}
private:
int mem;
};
void fun();
and then we can call this function, just like any regular function.
fun();
Can someone explain about (with examples):
In what cases do we need to define friend function inside the class
definition.
What is special about this kind of definition which can not be
achieved with just declaring function as friend in class and then
defining the function outside.
Assuming that you already know what is a friend function, there is absolutely no special meaning to your example: what you have is a regular friend function, with its declaration and definition combined.
Recall that friendship needs to be declared inside the class that "friends" a function. After that, the function can be defined at some place, for which you have two choices:
Outside the class - that is the common way of defining a friend function, or
Inside the class - that is what your example has.
Basic considerations for going with one approach vs. the other are the same as the rules that you use to decide between defining a member-function inside or outside the class.
Related
This question already has answers here:
When should I write the keyword 'inline' for a function/method?
(16 answers)
Closed 1 year ago.
I've been reading a c++ book, C++ Primer, and i was going through the class features and everything, and i encountered that, in a class most functions ( or every) are inline automatically.
What difference does it really make? explicitly defining an inline function vs implicitly defining an inline function, we could have already overloaded them inside the class scope anyways, i am finding it very difficult to understand this part. Is there any kind of performance gain by doing explicitly ?. in the photo we can see get() function using both methods explicit & implicit, can someone clarify me here.
I have to edit the question, because I've been told so many times that class members functions are not automatically inline. But the book c++ primer and other internet sources say that they are automatically inline.
Here is a piece of text from the book..
If a definition of a member function is within the class body it is implicitly inline. If you only declare it in the class body and you place the definition outside of it you need to make it explicitly inline.
This can be done in two ways:
struct test {
inline void foo();
};
void test::foo() {
}
Or
struct test {
void foo();
};
inline void test::foo() {
}
While both work, the second option is generally recommended.
This question already has answers here:
Why does decltype not see the member declaration? [duplicate]
(3 answers)
Closed 5 years ago.
Why this code is incorrect?
class Method
{
public:
Method(decltype(info2) info1);
virtual ~Method(){}
protected:
QSharedPointer<info> info2;
};
But this code is correct:
class Method
{
public:
virtual ~Method(){}
protected:
QSharedPointer<info> info2;
public:
Method(decltype(info2) info1);
};
why place of class constructor is important?
I thought that place of definition class constructor isnt important.
I believe this part of the standard is relevant [basic.scope.class]/1.1:
The potential scope of a name declared in a class consists not only of the declarative region following the
name’s point of declaration, but also of all function bodies, default arguments,
exception-specification
s,
and
brace-or-equal-initializers
of non-static data members in that class (including such things in nested
classes).
Note that it only mentions default arguments. So this works since the decltype is referred in a default argument:
Method(QSharedPointer<int> info1 = decltype(info2)())
And this also works since it's inside a body:
Method(<...>)
{
decltype(info2) info3;
}
However your example does not work because such a placement of a decltype is not covered by the paragraph I quoted, thus the name info2 is considered out of scope.
Place of QSharedPointer info2;
is important.
'info2' should be defined before using it into decltype (http://en.cppreference.com/w/cpp/language/decltype).
Next would not work either:
void f() {
d();
}
void d() {
}
This question already has answers here:
Defining a member function inside the class instead of outside in C++?
(4 answers)
Closed 7 years ago.
Is it true that if a function body is defined inside a class, the compiler will mark it inline? (even if not marked by the writer)
example:
class F {
public:
void func() {
std::cout << "is this inline?\n";
}
};
Yes.
[C++14: 9.3/2]: A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition. [..]
However, whether this has any observable effects beyond the associated linkage requirements is only as predictable as the inline keyword ever is.
The reason for this rule is so that it is legal to include the class definition — member functions and all — via a header into multiple translation units. You would have multiple reference linker errors otherwise.
If you state the body of a function inside a class, it is equivalent to defining that function outside the class and prefixing it with the inline keyword, but whether or not it will be inlined is ultimately up to the compiler.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i do not mean friend function. I guess there is not a way to achieve this in C++ like any other languages. Am I right?
The background:
I have a helper function for a class A. I know there are a couple of options here
(1) to declare it in the same namespace as class A is in
(2) put it as a static function of class A.
I was just thinking if it is possible to do the following which is kind of the combination of the above two. i.e.,
I want that function to be right under the scope of A but not as a member function of class A.
I hope this is clearer now. Thanks a lot.
You mean a static member function.
class T
{
static void lol();
};
void T::lol()
{
/* ... */
}
int main()
{
T::lol();
}
This isn't really "in the scope of the class" any more than a free function would be, because the only thing that can really mean is access to member variables, and access to member variables requires an instance.
But this is the closest to what you've asked.
Do you mean like a static function?
class Foo
{
void memberFunction();
static void classFunction();
};
Foo foo;
foo.memberFunction(); // called as a member function
Foo::classFunction(); // called on the class, no object necessary
That's basically a function in the C++ "scope". I'm not sure what you mean by scope though. If you just mean namespace, then this is it.
You can declare it as a static function - that is a function that is defined against the class but which does not require access to any particular instance of that class's member variables:
static int foobar()...
Which is then called with
Foo::foobar();
If you want to limit the scope of the function so it can ONLY be called from inside the class, you can make it private much like any other member function.
If you wish, there's nothing stopping you from creating a standard member function and just not accessing any member variables...
The way I usually do this is to declare a function in the .c++ file with 'static' qualifier. Please realize that is different from the normal use of c++ 'static' functions.
If you declare in 'bar.c++' (not 'bar.h++'):
static void foo() {}
...it is a function which is not 'visible' outside its object file. I.e. a local helper function.
So to be precise:
bar.h++;
class bar
{
void f();
};
bar.c++;
#include "bar.h++"
static void foo() {}
void bar::f() { foo(); }
Cheers!
You can declare a nested class:
class A
{
class X
{
void helper(A object)
{
std::cout << object.member;
}
};
int member;
};
Here helper is a member function of class Y, not of class A. I am not sure how this helps you though.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to declare a friend class conditionally?
This question branches off from Can friend class be declared conditionally in C++03?. Specifically, does C++11 provide any additional options to help with conditionally declaring a friend class? That is, is it at all possible to do this in C++11?
Going through cplusplus.com, I came across std::enable_if. I tried using it, but could not figure out the right syntax. Is std::enable_if the right construct to use for this purpose? Below is the code I tried, based on the example given there. I do not really need a template here, but I do not know how to avoid it, since all the example codes given there use it.
class Foo {
template<typename T> struct std::enable_if<true, T> {
typedef T Bar;
friend class Bar;
};
};
This gives the following compile error message:
prog.cpp:5:36: error: 'enable_if' is not a template
prog.cpp:5:55: error: qualified name does not name a class before '{' token
Edit Just to make this more easily visible, as mentioned below in the comment: This requirement is unusual. This is part of a new research project in hardware simulation, that I am working on. The testbench is written in C++, and I want to display the variables in a waveform. I have researched various other options, and figured out that I need to use a friend class, due to practical considerations. The friend will capture the values and generate the waveform, but I would prefer to have the friend only when the waveform is required, and not all the time.
[class.friend]/3 tells this :
A friend declaration that does not declare a function shall have one of the following forms:
friend elaborated-type-specifier ;
friend simple-type-specifier ;
friend typename-specifier ;
therefore it is not possible to conditionally declare friends of a class.
Actually, you can do it with a macro :
class Foo {
#ifdef DECLARE_A_FRIEND
friend class Bar;
#endif
};
};
and then define or undefine the macro as a compilation parameter.