Someone told me that there is a difference between declaring a friend class in the public or private areas of the class, but I can't seem to find anything about this online, and I'm not sure they knew what they were talking about.
I mean the difference between:
class A
{
public:
friend class B;
};
and
class A
{
private: //or nothing as the default is private
friend class B;
};
Is there a difference?
The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.
As such access specifiers have no effect on the meaning of friend declarations (they can appear in private: or in public: sections, with no difference).
No, there's no difference - you just tell that class B is a friend of class A and now can access its private and protected members, that's all.
Since the syntax
friend class B
doesn't declare a member of the classA
, so it doesn't matter where you write it, classB
is a friend of classA
.Also, if you write
friend class B
inprotected
section ofA
, then it does NOT mean thatB
can access onlyprotected
andpublic
members ofA
.Always remember that once
B
becomes a friend ofA
, it can access any member ofA
, no matter in which section you writefriend class B
.