1. 友元的概念
友元的目的就是让一个函数或者类 访问另一个类中私有成员。
友元的三种实现:
- 全局函数做友元
- 类做友元
- 成员函数做友元
2. 友元的实现方式
2.1 全局函数做友元
#include <iostream>
using namespace std;
class Building
{// 告诉编译器 goodGay 全局函数 是 Building类的好朋友,可以访问类中的私有内容friend void goodGay(Building *building);public:Building(){this->m_SittingRoom = "客厅";this->m_BedRoom = "卧室";}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 b;goodGay(&b);return 0;
}
2.2 类做友元
#include<iostream>
using namespace std;
class Building;
class goodGay
{
public:goodGay();void visit();private:Building *building;
};class Building
{// 告诉编译器 goodGay类是Building类的好朋友,可以访问到Building类中私有内容friend class goodGay;
public:Building();public:string m_SittingRoom; // 客厅
private:string m_BedRoom; // 卧室
};Building::Building()
{this->m_SittingRoom = "客厅";this->m_BedRoom = "卧室";
}goodGay::goodGay()
{building = new Building;
}void goodGay::visit()
{cout << "好基友正在访问" << building->m_SittingRoom << endl;cout << "好基友正在访问" << building->m_BedRoom << endl;
}int main()
{goodGay gg;gg.visit();return 0;
}
2.3 成员函数做友元
#include <iostream>
using namespace std;
class Building;
class goodGay
{
public:goodGay();void visit(); // 只让visit函数作为Building的好朋友,可以发访问Building中私有内容void visit2();private:Building *building;
};
class Building
{// 告诉编译器 goodGay类中的visit成员函数 是Building好朋友,可以访问私有内容friend void goodGay::visit();public:Building();public:string m_SittingRoom; // 客厅
private:string m_BedRoom; // 卧室
};
Building::Building()
{this->m_SittingRoom = "客厅";this->m_BedRoom = "卧室";
}
goodGay::goodGay()
{building = new Building;
}
void goodGay::visit()
{cout << "好基友正在访问" << building->m_SittingRoom << endl;cout << "好基友正在访问" << building->m_BedRoom << endl;
}
void goodGay::visit2()
{cout << "好基友正在访问" << building->m_SittingRoom << endl;// cout << "好基友正在访问" << building->m_BedRoom << endl;
}int main()
{goodGay gg;gg.visit();return 0;
}