#include<bits/stdc++.h> using namespace std; //类做友元 class Building; class GoodGay{ public: GoodGay();//初始化构造函数 void visit();//参观函数,访问Building中的属性 Building *building; }; class Building{ //GoodGay是本类的好朋友,可以访问本类的私有成员 friend class GoodGay; 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<<"好基友类正在访问: "<<building->m_SittingRoom <<endl; cout<<"好基友类正在访问: "<<building->m_BedRoom <<endl; } void test(){ GoodGay gg; gg.visit(); } int main() { test(); return 0; }