01.全局函数做友元
#include<iostream>
using namespace std;
class Building
{
friend void goodGay(Building& building);
public:
Building()
{
this->m_BedRoom = "卧室";
this->m_SittingRoom = "客厅";
}
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
void goodGay(Building& building)
{
cout << "杰哥正在访问:" << building.m_SittingRoom << endl;
cout << "杰哥正在访问:" << building.m_BedRoom << endl;
}
int main()
{
Building building;
goodGay(building);
system("pause");
}
02.成员函数做友元
#include<iostream>
using namespace std;
class Building;
class GoodGay
{
public:
GoodGay();
void visit();
void visit2();
private:
Building* building;
};
class Building
{
friend void GoodGay::visit();
public:
Building();
public:
string m_SittingRoom;
private:
string m_BedRoom;
};
Building::Building() {
m_SittingRoom = "客厅";
m_BedRoom = "卧室";
}
GoodGay::GoodGay()
{
building = new Building;
}
void GoodGay::visit()
{
cout << "visit函数正在访问:" << building->m_SittingRoom << endl;
cout << "visit函数正在访问:" << building->m_BedRoom<< endl;
}
void GoodGay::visit2()
{
cout << "visit2函数正在访问:" << building->m_SittingRoom << endl;
}
void test01() {
GoodGay gg;
gg.visit();
gg.visit2();
}
int main()
{
test01();
system("pause");
}