Reputation: 17223
I have the following code
class foo
{
public:
foo() {}
private:
int foo_int;
friend class bar; //----->Statement A
};
class bar
{
public:
void someMethod()
{
foo f;
f.foo_int = 13;
}
};
Now I also read this answer on SO. However I cant put the pieces of puzzle together as to why the compiler recognizes bar as a type. I was under the impression that it would complain of Bar being an incomplete type however that did not happen. My question is why ?
Upvotes: 0
Views: 576
Reputation: 96810
A friend
specification of a class that has not been declared yet acts as a declaration of the class. It is perfectly fine to declare an incomplete type as a friend of a class because the compiler only needs to know about the type being declared.
Upvotes: 1
Reputation: 16406
friend class bar;
is simply a declaration ... there's nothing for the compiler to complain about. The limitation on incomplete types is when the compiler needs information about the type that it doesn't have, such as its size or, for base classes, its members, but for friend
it doesn't need anything other than its name.
Note that it doesn't matter where the friend
declaration occurs in the class definition ... that it follows private:
doesn't make it private. It's better to put it at the top of the class definition.
Upvotes: 2