• C++友元(friend)


    友元

    友元 friend 机制允许一个类授权其他的函数访问它的非公有成员.
    
    友元声明以关键字 friend 开头 ,它只能出现在类的声明中, 它们不受其在类体中的 public private 和
    protected 区的影响.
    
    • 1
    • 2
    • 3
    • 4

    友元分为外部函数友元, 成员函数友元,类友元

    特点

    不具有对称性:A 是 B 的友元, 并不意味着 B 是A的友元
    不具有传递性:A是B的友元, B是C的友元, 但A不是C的友元。
    不具有继承性: Base 类型继承 Object类型, 如果Object 类型是A的友元,但Base类型不是A友
    元。

    外部函数友元:

    示例
    需要在类中进行对其进行声明,则可以访问类的所有成员

    class Int
    {
    int value;
    public:
    Int(int x = 0):value(x)
    {
    cout<<"Create Int: "<<this<<endl;
    }
    ~Int(){ cout<<"Destroy Int: "<<this<<endl;}
    friend void Print(const Int &it); // 注册为类的友元函数
    };
    void Print(const Int &it)
    {
    cout<<it.value<<endl;
    }
    int main()
    {
    Int a(10);
    Print(a);
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    成员函数友元
    将一个成员函数声明成友元的时,必须要指明该成员函数属于哪个类:

    class Object; // 类的声明
    class Int
    {
    friend void Object::Print(const Int &it); // 注册为成员函数友元
    private:
    int value;
    public:
    Int(int x = 0):value(x){ cout<<"Create Int: "<<this<<endl;}
    ~Int(){ cout<<"Destroy Int: "<<this<<endl;}
    };
    class Object
    {
    public:
    void Print(const Int &it)
    {
    cout<<it.value<<endl;
    }
    };
    int main()
    {
    Int a(10);
    Object obj;
    obj.Print(a);
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    总结:
    1.友元函数不是类的成员函数,在函数体中访问对象的成员,必须用对象名加运算符“.”加对象成员
    名。但友元函数可以访问类中的所有成员,一般函数只能访问类中的公有成员。
    2.友元函数不受类中的访问权限关键字限制,可以把它放在类的公有、私有、保护部分,但结果一
    样。
    3.某类的友元函数的作用域并非该类作用域。如果该友元函数是另一类的成员函数,则其作用域为
    另一类的作用域,否则与一般函数相同。
    类友元

    整个类可以是另一个类的友元。友元类的每个成员函数都是另一个类的友元函数,都可访问另一个
    类中的所以成员,共有,保护或私有数据成员

    class Object; // 类的声明
    class Int
    {
    friend class Object; // 注册为类友元
    private:
    int value;
    public:
    Int(int x = 0):value(x){ cout<<"Create Int: "<<this<<endl;}
    ~Int(){ cout<<"Destroy Int: "<<this<<endl;}
    };
    class Object
    {
    public:
    void Print(const Int &it)
    {
    cout<<it.value<<endl;
    }
    };
    int main()
    {
    Int a(10);
    Object obj;
    obj.Print(a);
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    计算机视觉与深度学习实战,Python为工具,小波变换的数字水印技术
    Java设计模式之组合模式
    Hydra参数
    用命令行获取操作系统名称版本+CPU等信息
    PO VO DTO 转换神器替代BeanUtils 了
    兼容vue2和vue3版本的插件实现过程
    什么是JUC_java培训
    (附源码)node.js物资管理系统 毕业设计 071130
    l8-d6 socket套接字及TCP的实现框架
    Python函数(一)
  • 原文地址:https://blog.csdn.net/qq_42795061/article/details/125456796