请考虑以下代码: 如果假设我要使这两个类互相访问私有成员,那么这将如何发生? 我不能简单地将卡车上的班级移到公共汽车上,因为这将再次给我错误,因为它不会在卡车上找到被声明为朋友的公共汽车。
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;
}
提前致谢。