友元函数和友元类

1、友元介绍ios

咱们知道,类的成员函数能够访问同类的其余成员函数,包括公有、私有和保护成员。而类的外部函数只能访问类的公有成员。编程

友元是一种容许非类成员函数访问类的非公有成员的一种机制。
能够把一个函数指定为类的友元,也能够把整个类指定为另外一个类的友元。
函数

友元函数spa

友元类设计


2、友元函数对象

友元函数在类做用域外定义,但它须要在类体中进行说明
为了与该类的成员函数加以区别,定义的方式是在类中用关键字friend说明该函数,格式以下:
继承

friend  类型 友元函数名(参数表);ci

友元的做用在于提升程序的运行效率
作用域

友元函数注意事项:io

一、友元函数不是类的成员函数,在函数体中访问对象的成员,必须用对象名加运算符“.”加对象成员名。但友元函数能够访问类中的全部成员(公有的、私有的、保护的),通常函数只能访问类中的公有成员。
二、友元函数不受类中的访问权限关键字限制,能够把它放在类的公有、私有、保护部分,但结果同样。
三、某类的友元函数的做用域并不是该类做用域。若是该友元函数是另外一类的成员函数,则其做用域为另外一类的做用域,不然与通常函数相同。
四、友元函数破坏了面向对象程序设计类的封装性,因此友元函数如不是必须使用,则尽量少用。或者用其余手段保证封装性。

 C++ Code 
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
26
27
28
29
30
31
32
33
34
35
36
37
 
#include <math.h>
#include <iostream>
using namespace std;

class Point
{
    friend double Distance(const Point &p1, const Point &p2);
public:
    Point(int x, int y);
private:

    int x_;
    int y_;
};

Point::Point(int x, int y) : x_(x), y_(y)
{

}

double Distance(const Point &p1, const Point &p2)
{
    double dx = p1.x_ - p2.x_;
    double dy = p1.y_ - p2.y_;

    return sqrt(dx * dx + dy * dy);
}

int main(void)
{
    Point p1(3, 4);
    Point p2(6, 9);

    cout << Distance(p1, p2) << endl;
    return 0;
}

程序中Distance 是Point类的友元函数,能够访问类的私有数据成员。


3、友元类

若是某类B的成员函数会频繁的存取另外一个类A的数据成员, 而A的数据成员的Private/Protectd限制形成B存取的麻烦, B只能经过A的Public的成员函数进行间接存取
把B作成A类的友元类,即A类向B类开放其Private/Protectd内容, 让B直接存取
友元类:一个类能够做另外一个类的友元
友元类的全部成员函数都是另外一个类的友元函数
友元类的声明:
    friend class 类名;

友元类注意事项:

一、友元关系是单向的
二、友元关系不能被传递
三、友元关系不能被继承

TeleController.h :

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
#ifndef  _TELE_CONTROLLER_H_
#define _TELE_CONTROLLER_H_

class Television;

class TeleController
{
public:
    void VolumeUp(Television &tv);

    void VolumeDown(Television &tv);

    void ChanelUp(Television &tv);

    void ChanelDown(Television &tv);
};

#endif // _TELE_CONTROLLER_H_

TeleController.cpp :

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
#include "TeleController.h"
#include "Television.h"

void TeleController::VolumeUp(Television &tv)
{
    tv.volume_ += 1;
}

void TeleController::VolumeDown(Television &tv)
{
    tv.volume_ -= 1;
}

void TeleController::ChanelUp(Television &tv)
{
    tv.chanel_ += 1;
}

void TeleController::ChanelDown(Television &tv)
{
    tv.volume_ -= 1;
}

Television.h:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
#ifndef _TELEVISION_H_
#define _TELEVISION_H_

class TeleController;

class Television
{
    friend class TeleController;
public:
    Television(int volume, int chanel);
private:
    int volume_;
    int chanel_;
};

#endif // _TELEVISION_H_

Television.cpp:

 C++ Code 
1
2
3
4
5
6
 
#include "Television.h"

Television::Television(int volume, int chanel) : volume_(volume), chanel_(chanel)
{

}

main.cpp:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
#include "Television.h"
#include "TeleController.h"
#include <iostream>
using namespace std;


int main(void)
{
    Television tv(1, 1);

    TeleController tc;
    tc.VolumeUp(tv);

    return 0;
}

TeleController 类做为Television类的友元类,这样TeleController 类的成员函数就均可以访问Television类的全部成员,包括私有。


参考:

C++ primer 第四版 Effective C++ 3rd C++编程规范

相关文章