其他分享
首页 > 其他分享> > 友元成员函数

友元成员函数

作者:互联网

友元函数中成员函数使用

#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;
}

标签:友元,函数,成员,bedRoom,visit,class,BedRoom,GoodGay,void
来源: https://www.cnblogs.com/yoshinb/p/15018470.html