//在程序里,有些私有的属性 也想让类外特殊的一些函数或者类进行访问,就需要用到友元技术
//友元的目的就是让一个函数或者类 访问另一个类中私有成员
//友元关键字为 friend
#include<iostream>
using namespace std;
class Building
{
friend class GoodGay;//把class GoodGay类整体复制过来,在前面加上 friend 就可以本类私有成员对象了
public:
Building();//Building类内声明
public:
string m_SittingRood;
private:
string m_BedRoom;
};
class GoodGay
{
public:
GoodGay();//GoodGay类内声明
void Visit();//Visit类内声明
Building* building;
};
GoodGay::GoodGay()//GoodGay类外实现
{
building = new Building;
}
Building::Building()//Building类外实现
{
this->m_SittingRood = "客厅";
this->m_BedRoom = "卧室";
}
void GoodGay::Visit()//Visit类外实现
{
cout << "好基友正在访问你的" << building->m_SittingRood << endl;
cout << "好基友正在访问你的" << building->m_BedRoom << endl;
}
void test01()
{
GoodGay gg;
gg.Visit();
}
int main()
{
test01();
return 0;
}