6

Possible Duplicate:
Friend scope in C++

Are friends in C++ mutual?

Community
  • 1
  • 1
Liu
  • 645
  • 2
  • 8
  • 16
  • 13
    Just like in real life: one-way relationships exist, even when you believe they are mutual :) – ereOn Oct 13 '10 at 08:31
  • 2
    This question made me smile :D – jamiebarrow Oct 13 '10 at 08:46
  • Friend is already a questionable construct and can cause code to be untidy from an access point of view. At least making it one way reducing the degree that the protection is compromised – Elemental Oct 13 '10 at 09:52

2 Answers2

10
class bar
{
private:
   void barMe();
};

class foo
{
private:
   void fooMe();

friend bar;
};

In the above example foo class can't call barMe() You need to define the classes this way in order that the friend be mutual:

class foo; // forward
class bar
{
private:
   void barMe();

friend foo;
};

class foo
{
private:
   void fooMe();

friend bar;
};
Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87
4

The friend relationship is only one-way in general - but there is nothing to stop you declaring Class A a friend of class B AND class B a friend of class A. So a mutual relationship can be established

Elemental
  • 7,365
  • 2
  • 28
  • 33