作用:让一个函数或者类,去访问另一个类中的私有成员
关键字:friend
三种实现方法:
● 全局函数做友元
● 类做友元
● 成员函数做友元
1.全局函数做友元
例子:
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Building{ 6 //允许通过void goodFriend()这个函数访问Building类的私有成员 7 //缺少这段,void goodFriend()这个函数就不能访问Building类的私有成员 8 //这段只需要写在类的顶端,不需要设置权限 9 friend void goodFriend(Building *building); 10 public: 11 Building(){ 12 m_SittingRoom = "客厅"; 13 m_BedRoom = "卧室"; 14 } 15 16 public: 17 string m_SittingRoom; 18 private: 19 string m_BedRoom; 20 }; 21 22 //全局函数void goodFriend(Building *building) 23 void goodFriend(Building *building){ 24 cout << "全局函数正在访问:" << building->m_SittingRoom << endl; 25 cout << "全局函数正在访问:" << building->m_BedRoom << endl; 26 } 27 28 void test01(){ 29 Building building; 30 goodFriend(&building); 31 } 32 33 int main(){ 34 test01(); 35 system("pause"); 36 return 0; 37 }
2.类做友元
例子(此处采用类内声明函数,类外定义成员函数的方法):
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Building; //声明类 6 ///////////////// 7 class GoodFriend{ 8 public: 9 GoodFriend(); 10 void visit(); //用于访问Building中的属性 11 Building * building; 12 }; 13 ///////////////// 14 class Building{ 15 friend class GoodFriend; 16 public: 17 Building(); //类内声明成员函数 18 public: 19 string m_SittingRoom; 20 private: 21 string m_BedRoom; 22 }; 23 //////////////////////// 24 //类外写成员函数 25 Building::Building(){ 26 m_SittingRoom = "客厅"; 27 m_BedRoom = "卧室"; 28 } 29 GoodFriend::GoodFriend(){ 30 building = new Building; //building指针指向新开辟的堆区 31 } 32 void GoodFriend::visit(){ 33 cout << "好朋友正在访问:" << building->m_SittingRoom << endl; 34 cout << "好朋友正在访问:" << building->m_BedRoom << endl; 35 } 36 /////////////////////////// 37 void test01(){ 38 GoodFriend gg; 39 gg.visit(); 40 } 41 42 int main(){ 43 test01(); 44 system("pause"); 45 return 0; 46 }
3.成员函数做友元
例子(此处采用类内声明函数,类外定义成员函数的方法):
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Building; //声明 6 7 class GoodFriend{ 8 public: 9 GoodFriend(); 10 void visit(); //可以访问Building中的私有成员 11 void visit2(); //不可以访问Building中的私有成员 12 Building * building; 13 }; 14 15 class Building{ 16 //确实了作用域GoodFriend,那么visit就是全局变量下的函数 17 friend void GoodFriend::visit(); //告诉编译器GoodFriend类下的函数visit可以访问本类的私有成员 18 public: 19 Building(); //构造函数 20 21 public: 22 string m_SittingRoom; 23 private: 24 string m_BedRoom; 25 }; 26 27 //类外实现成员函数 28 Building::Building(){ //赋初值 29 m_SittingRoom = "客厅"; 30 m_BedRoom = "卧室"; 31 } 32 GoodFriend::GoodFriend(){ 33 building = new Building; //创建一个新的堆区让building这个指针指向它 34 } 35 36 void GoodFriend::visit(){ 37 cout << "visit正在访问:" << building->m_SittingRoom << endl; 38 cout << "visit正在访问:" << building->m_BedRoom << endl; 39 } 40 void GoodFriend::visit2(){ 41 cout << "visit2正在访问:" << building->m_SittingRoom << endl; 42 //cout << "visit正在访问:" << building->m_BedRoom << endl; 43 } 44 45 ///////////////////////////////// 46 void test01(){ 47 GoodFriend gg; 48 gg.visit(); 49 gg.visit2(); 50 } 51 int main(){ 52 test01(); 53 system("pause"); 54 return 0; 55 }
标签:友元,Building,GoodFriend,对象,void,BedRoom,SittingRoom,函数 From: https://www.cnblogs.com/MorningMaple/p/16875492.html