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;
    }
  • 相关阅读:
    metal的gpu query
    体积雾 global fog unity 及改进
    hdr rt format对颜色的影响
    unity deferred lighting
    unity linear space时 photoshop blend的正确设置
    unity linear work flow
    一些数据 bandwidth之类
    deferred rendering with msaa
    unity 显示mipmaplevel
    【转】在C#中使用SendMessage
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12096437.html
Copyright © 2011-2022 走看看