开发者

Are friends in c++ mutual? [duplicate]

开发者 https://www.devze.com 2023-01-19 23:27 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Friend scope in C++开发者_运维问答
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Friend scope in C++

开发者_运维问答

Are friends in C++ mutual?


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;
};


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

0

精彩评论

暂无评论...
验证码 换一张
取 消