1 友元
采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口。但是,有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类的数据成员,这时可以将这些函数定义为该函数的友元函数。除了友元函数外,还有友元类,两者统称为友元。友元的作用是提高了程序的运行效率(即减少了类型检查和安全性检查等都需要时间开销),但它破坏了类的封装性和隐藏性,使得非成员函数可以访问类的私有成员。
友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类。
1.1 友元函数
友元函数是可以直接访问类的私有成员的非成员函数。它是定义在类外的普通函数,它不属于任何类,但需要在类的定义中加以声明,声明时只需在友元的名称前加上关键字friend,其格式如下:
friend 类型 函数名(形参);
一个函数可以是多个类的友元函数,只需要在各个类中分别声明。
1.1.1 全局函数作为友元函数
#include <iostream>
#include <string.h>
using namespace std;
class Buillding
{
// 全局函数作友元函数
friend void Goodfriend(const Buillding &buillding);
public:
Buillding()
{
m_Parlour = "客厅";
m_Bedroom = "卧室";
}
public:
string m_Parlour; // 客厅
private:
string m_Bedroom; // 卧室
};
void Goodfriend(const Buillding &buillding)
{
// 访问公有的
cout << "正在访问:" << buillding.m_Parlour << endl;
// 访问私有的
cout << "正在访问:" << buillding.m_Bedroom << endl;
}
int main()
{
Buillding b;
// 调用Buillding类的友元函数
Goodfriend(b);
return 0;
}

1.1.2 类中的成员函数作为友元函数
#include <iostream>
#include <string.h>
using namespace std;
// 前向声明是一种常用的不完整类型(只给出了声明,没有给出定义),仅可用于定义指针或者引用。
class Buillding;
class Goodfriend
{
public:
void show(const Buillding &buillding);
};
class Buillding
{
// 类的成员函数作友元函数
friend void Goodfriend::show(const Buillding &buillding);
public:
Buillding()
{
m_Parlour = "客厅";
m_Bedroom = "卧室";
}
public:
string m_Parlour; // 客厅
private:
string m_Bedroom; // 卧室
};
// class Buillding前向声明,仅可用于定义指针或者引用,因此show函数实现不能写在Goodfriend类中
void Goodfriend::show(const Buillding &buillding)
{
// 访问公有的
cout << "正在访问Buillding类中:" << buillding.m_Parlour << endl;
// 访问私有的
cout << "正在访问Buillding类中:" << buillding.m_Bedroom << endl;
}
int main()
{
Buillding b;
Goodfriend g;
// 调用Buillding类的友元函数
g.show(b);
return 0;
}

1.2 友元类
友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。
当希望一个类可以存取另一个类的私有成员时,可以将该类声明为另一类的友元类。
定义友元类的语句格式如下:
friend class 类名;
// friend 和 class 是关键字,类名必须是程序中的⼀一个已定义过的类。
// 以下语句说明类B是类A的友元类
class A
{
friend class B;
};
例子:类Goodfriend是类Buillding的友元类
#include <iostream>
#include <string.h>
using namespace std;
// 前向声明是一种常用的不完整类型(只给出了声明,没有给出定义),仅可用于定义指针或者引用。
class Buillding;
class Goodfriend
{
public:
void show(const Buillding &buillding);
};
class Buillding
{
// 友元类
friend class Goodfriend;
public:
Buillding()
{
m_Parlour = "客厅";
m_Bedroom = "卧室";
}
public:
string m_Parlour; // 客厅
private:
string m_Bedroom; // 卧室
};
// class Buillding前向声明,仅可用于定义指针或者引用,因此show函数实现不能写在Goodfriend类中
void Goodfriend::show(const Buillding &buillding)
{
// 访问公有的
cout << "正在访问Buillding类中:" << buillding.m_Parlour << endl;
// 访问私有的
cout << "正在访问Buillding类中:" << buillding.m_Bedroom << endl;
}
int main()
{
Buillding b;
Goodfriend g;
g.show(b);
return 0;
}
2 友元浅析
2.1 声明位置
友元声明以关键字 friend 开始,它只能出现在类定义中。因为友元不是授权类的成员,所以它不受其所在类的声明区域public、private 和 protected 的影响。通常我们选择把所有友元声明组织在一起并放在类头之后。
2.2 友元利弊
友元不是类成员,但是它可以访问类中的私有成员。友元的作用在于提高程序的运行效率,但是,它破坏了类的封装性和隐藏性,使得非成员函数可以访问类的私有成员。不过,类的访问权限确实在某些应用场合显得有些呆板,从而容忍了友元这一特别语法现象。
2.3 注意事项
- 友元关系不能被继承。
- 友元关系是单向的,不具有交换性。若类B是类A的友元,类A不一定是类B的友元,要看在类中是否有相应的声明。
- 友元关系不具有传递性。若类B是类A的友元,类C是B的友元,类C 不一定是类A的友元,同样要看类中是否有相应的声明。
本文围绕 C++ 中的友元展开,介绍了友元函数(包括全局函数和类中成员函数作为友元函数)和友元类,它们能直接访问类的私有成员。还分析了友元的声明位置、利弊,指出其虽提高运行效率,但破坏类的封装性。同时说明了友元关系不继承、单向且无传递性。
2150

被折叠的 条评论
为什么被折叠?



