//类做友元类:
class Building
{
friend class goodGay;
public:
Building();
public:
string m_sittingroom; //客厅
private:
string m_bedroom; //卧室
};
Building::Building()
{
this->m_bedroom = "卧室";
this->m_sittingroom = "客厅";
}
class goodGay
{
public:
goodGay();
void visit();
private:
Building * building;
};
//类内声明,类外实现;
goodGay::goodGay()
{
building = new Building;
}
void goodGay::visit()
{
cout<<"基友正在"<<this->building->m_sittingroom<<endl;
//要访问私有属性的元素;
//在Building 类中定义友元类:friend class goodGay;
cout << "基友正在" << this->building->m_bedroom << endl;
}
void test01()
{
goodGay g1;
g1.visit();
}
int main()
{
test01();
return 0;
}