成员函数作为友元
#include <iostream>
using namespace std;
class Building;
class GoodGay
{
public:
GoodGay();
void visit01();//让visit01函数可以访问Building中的私有成员
void visit02();//让visit02函数不可以访问Building中的私有成员
Building* building;
};
class Building
{
//GoodGay类下的visit01成员函数作为本类的好朋友,可以访问本类的私有成员
friend void GoodGay::visit01();
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
GoodGay::GoodGay()
{
building = new Building;
}
Building::Building()
{
m_BedRoom = "卧室";
m_SittingRoom = "客厅";
}
void GoodGay::visit01()
{
cout << "visit01正在访问你的:" << building->m_SittingRoom << endl;
cout << "visit01正在访问你的:" << building->m_BedRoom << endl;
}
void GoodGay::visit02()
{
cout << "visit02正在访问你的:" << building->m_SittingRoom << endl;
}
void test01()
{
GoodGay print;
print.visit01();
print.visit02();
}
int main()
{
test01();
system("pause");
return 0;
}