zoukankan      html  css  js  c++  java
  • c++之友元

    客厅:客人可以随意走动;卧室:只有自己能进去;

    现在想要自己的好朋友可以进去,就需要用到友元技术。即友元的目的就是让一个函数或者类访问另一个类中私有成员。

    关键字:friend

    三种实现方法:

    • 全局函数做友元
    • 类做友元
    • 成员函数做友元

    全局函数做友元:

    #include<iostream>
    using namespace std;
    class Building{
        friend void goodGay(Building* building);
    public:
        Building() {
            room = "客厅";
            myRoom = "卧室";
        }
    public:
        string room;
    private:
        string myRoom;
    };
    //全局函数
    void goodGay(Building *building) {
        cout << "好朋友正在访问" <<building->room<< endl;
        //如果不将dooGay变成Building的友元函数,这里就会报错
        cout << "好朋友正在访问" << building->myRoom << endl;
    }
    void test() {
        Building building;
        goodGay(&building);
    }
    int main() {
        test();
        system("pause");
        return 0;
    }

    类做友元:

    说明:在类外定义成员函数时,需要先在类中声明函数。利用类名::函数名(),可以定义类的成员函数。

    #include<iostream>
    using namespace std;
    
    class Building{
        friend class GoodGay;
    public:
        string room;
        Building();
    private:
        string myRoom;
    };
    //类外写成员函数
    Building::Building() {
        room = "客厅";
        myRoom = "卧室";
    }
    
    class GoodGay {
    public:
        Building* building;
        GoodGay();
        void visit();
    };
    GoodGay::GoodGay() {
        building = new Building();
    }
    void GoodGay::visit() {
        cout << "好基友类正在访问:" << building->room << endl;
        cout << "好基友类正在访问:" << building->myRoom << endl;
    }
    void test() {
        GoodGay gg;
        gg.visit();
    }
    int main() {
        test();
        system("pause");
        return 0;
    }

    成员函数做友元:

    #include<iostream>
    using namespace std;
    class Building;
    class GoodGay;
    
    //要先定义GoodGay类。。。
    //我尝试在Building之后定义,就不行,之前也先声明了这两个类
    class GoodGay {
    public:
        GoodGay();
        void visit();//让该函数可以访问Building的私有成员
        void visit2();
    private:
        Building* building;
    };
    
    class Building{
        friend void GoodGay::visit();
    public:
        string room;
        Building();
    private:
        string myRoom;
    };
    //类外写成员函数
    Building::Building() {
        room = "客厅";
        myRoom = "卧室";
    }
    
    GoodGay::GoodGay() {
        building = new Building();
    }
    void GoodGay::visit() {
        cout << "好基友类正在访问:" << building->room << endl;
        cout << "好基友类正在访问:" << building->myRoom << endl;
    }
    void GoodGay::visit2() {
        cout << "好基友类正在访问:" << building->room << endl;
        //cout << "好基友类正在访问:" << building->myRoom << endl;
    }
    void test() {
        GoodGay gg;
        gg.visit();
    }
    void test2() {
        GoodGay gg;
        gg.visit2();
    }
    int main() {
        test();
        system("pause");
        return 0;
    }
  • 相关阅读:
    VS2010 枚举注释任务
    osg例子中文翻译,半机翻
    怎么愉快地添加目标位置?
    变更路线节点。妈妈,我的强迫症有救啦!
    测试必备工具之抓包神器 Charles 如何抓取 https 数据包?
    测试必备工具之最强抓包神器 Charles,你会了么?
    ‘员工拒绝加班被判赔偿公司 1.8 万元’,作为测试猿你怕了么?
    全网最全测试点总结:N95 口罩应该如何测试?
    测试角度:如何看待三星大量手机系统崩溃并数据丢失事件?
    男生 vs 女生,谁更加适合做软件测试?
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12096437.html
Copyright © 2011-2022 走看看