C++基础知识:友元

封装

封装(英语:Encapsulation)是指,一种将[抽象] “抽象化 (计算机科学)”)性函数接口的实现细节部分包装、隐藏起来的方法。

封装的优点:
- 确保用户代码不会无意间破坏封装对象的状态
- 被封装的类的具体实现细节可以随时改变,无需调整用户级别的代码。
摘自《C++primer》

友元

采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口。但是,有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类的数据成员,这时可以将这些函数定义为该函数的友元函数。除了友元函数外,还有友元类,两者统称为友元。友元的作用是提高了程序的运行效率(即减少了类型检查和安全性检查等都需要时间开销),但它破坏了类的封装性和隐藏性,使得非成员函数可以访问类的私有成员。

友元函数的定义方法:

在类里声明一个普通函数,在前面加上friend修饰,那么这个函数就成了该类的友元,可以访问该类的一切成员。

当一个函数需要访问两个不同类型对象的私有数据成员的时候,可以使用友元函数。有两种使用的方式:

  • 该函数作为全域函数,在两个类中被宣告为友谊函数
  • 作为一个类中的成员函数,在另一个类中被宣告为友谊函数

此外:
友元的声明只能在类定义的内部,但是具体位置不限。(一般,最好在类定义开始或者结束前的位置集中声明友元。)

举例说明:

#include <iostream>
using namespace std;

class Bezaa; // Forward declaration of class Bezaa in order for example to compile.
class Aazaa
{
private:
    int a;
public:
    Aazaa() { a = 0; }
    void show(Aazaa& x, Bezaa& y);
    friend void ::show(Aazaa& x, Bezaa& y); // declaration of global friend
};

class Bezaa
{
private:
    int b;
public:

    Bezaa() { b = 6; }
    friend void  ::show(Aazaa& x, Bezaa& y); // declaration of global friend
    friend void Aazaa::show(Aazaa& x, Bezaa& y); // declaration of friend from other class
};

// Definition of a member function of Aazaa; this member is a friend of Bezaa
void Aazaa::show(Aazaa& x, Bezaa& y)
{
    cout << "Show via function member of Aazaa" << endl;
    cout << "Aazaa::a = " << x.a << endl;
    cout << "Bezaa::b = " << y.b << endl;
}

// Friend for Aazaa and Bezaa, definition of global function
void show(Aazaa& x, Bezaa& y)
{
    cout << "Show via global function" << endl;
    cout << "Aazaa::a = " << x.a << endl;
    cout << "Bezaa::b = " << y.b << endl;
}

int main()
{
    Aazaa a;
    Bezaa b;

    show(a,b);
    a.show(a,b);
}

则输出为:
Show via global function
Aazaa::a = 0
Bezaa::b = 6
Show via function member of Aazaa
Aazaa::a = 0
Bezaa::b = 6

友元类的定义方法:

举例:

    class B {
        friend class A; // A is a friend of B

    private:
        int i;
    };

    class A {
    public:
        A(B b) {
            b.i = 0; // legal access due to friendship
        }
    };
友元类的性质:
  • 不相似
  • 不传递
  • 不继承
  • Friendships are not [symmetric]– if class A is a friend of class B, class B is not automatically a friend of class A.
  • Friendships are not [transitive] – if class A is a friend of class B, and class B is a friend of class C, class A is not automatically a friend of class C.
  • Friendships are not [inherited]– if class Base is a friend of class X, subclass Derived is not automatically a friend of class X; and if class X is a friend of class Base, class X is not automatically a friend of subclass Derived.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值