友元函数中成员函数使用
#include <iostream>
using namespace std;
class BedRoom; //声明一个类
class GoodGay {
public:
GoodGay();
~GoodGay();
void visit();
private:
BedRoom *bedRoom;
};
//在这里必须先实现类,在实现GoodGay,否则编译不通过
class BedRoom {
friend void GoodGay::visit();
public:
BedRoom(){
myBedRoom = "卧室";
};
~BedRoom(){};
private:
string myBedRoom;
};
GoodGay::GoodGay() {
bedRoom = new BedRoom();
}
GoodGay::~GoodGay() {
delete bedRoom;
}
void GoodGay::visit() {
cout<<"正在访问"<<bedRoom->myBedRoom<<endl;
}
int main() {
GoodGay gg;
gg.visit();
return 0;
}
友元成员函数