请考虑以下代码:如果假设我要使这两个类互相访问私有成员,那么这将如何发生?我不能简单地将卡车上的班级移到公共汽车前,因为这又会给我错误,因为它不会在卡车上找到声明为朋友的公共汽车。
class bus { private: int a; public: friend void truck:: disp(); }; class truck { private: int x; protected: int y; public: int z; friend class bus; void disp(); }; void bus :: print() { truck t; t.x = 10; t.y = 20; t.z = 30; cout<<t.x<<" "<<t.y<<" "<<t.z<<endl; } void truck :: disp() { bus b; b.a = 100; cout<<"Truck here"<<b.a<<endl; }
提前感谢。
请考虑以下代码:如果假设我要使这两个可访问彼此的私有成员的类,那将如何发生?我不能简单地在...
您可以更改bus
和truck
的定义顺序。 truck
本身的好友声明将向前声明bus
,因此您无需在bus
之前声明truck
。