#include<bits/stdc++.h> using namespace std; class Building{ //goodGay全局函数是Building的好朋友,可以访问Building 中的私有成员 friend void goodGay(Building *building); public: Building(){//初始化构造函数 m_SittingRoom="客厅"; m_BedRoom="卧室"; } public: string m_SittingRoom;//客厅 private: string m_BedRoom;//卧室 }; //全局函数 void goodGay(Building *building){ cout<<"好基友全局函数正在访问:"<<building->m_SittingRoom<<endl; cout<<"好基友全局函数正在访问:"<<building->m_BedRoom<<endl; } void test01(){ Building building; goodGay(&building); } int main() { //友元 //全局函数做友元 //类做友元 //成员函数做友元 test01(); return 0; }