1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include<iostream> using namespace std;
class Building { friend class GoodFriend; public: string m_sittingRoom; Building(); private: string m_bedRoom; }; class GoodFriend { public: GoodFriend(); void visit(); Building * buil; };
Building::Building() { m_sittingRoom = "客厅"; m_bedRoom = "卧室"; } GoodFriend::GoodFriend() { buil = new Building; } void GoodFriend::visit() { cout << "友元函数访问私有成员m_sittingRoom:" << buil->m_sittingRoom << endl; cout << "友元函数访问私有成员m_bedRoom:" << buil->m_bedRoom << endl; } void test01() { GoodFriend myfriend; myfriend.visit(); } int main() { test01(); return 0; }
|