One class can declare another class/method/function to be its friend:
class Foo { friend class Bar; // an entire class friend double Zip::zulu(int) const; // just one method friend int alpha(); // a free function friend std::ostream &operator<<(std::ostream &os, const Foo &); private: int x,y; };
class A
, via friend class B
, offers friendship to class B
.
C
is D
’s friend, then D
is not necessarily C
’s friend.
E
& F
are friends, and F
& G
are friends,
then E
& G
are strangers.
H
is I
’s friend, and I
is the parent of J
,
then H
& J
are strangers.
K
is L
’s parent, then K
& L
are strangers.
friend
declarations,
and overuse them.
friend
should be your last tool, not your first one.
friend
declarations to avoid the overhead
of method calls, then you have no faith in current compilers.
They’re quite good at inlining methods.