• 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
  • 相关阅读:
    【转】2022年“云计算的11类顶级威胁”报告
    ZZULIOJ1038: 绝对值最大
    java+jsp基于ssm汽车配件管理系统-计算机毕业设计
    Java网络编程
    计算机考研数据结构题库
    C语言sizeof函数解析
    Ubuntu——卸载、安装CUDA
    【IMX6ULL笔记】-- 从驱动到应用(基于Qt)- LED
    前端JavaScript中常见设计模式
    《FORTRAN语法:章节篇》第1章 数据类型
  • 原文地址:https://blog.csdn.net/qq_42795061/article/details/125456796