5.6 友元
前面讲过类具有封装和信息隐藏的特性。只有类的成员函数才能访问该类的私有成员,程序中的其他函数无法访问类中的私有成员。非成员函数可以访问类中的公有成员,但如果将数据成员都定义为公有的,又会破坏隐藏的特性。此外,在某些情况下,尤其是多次调用成员函数时,由于参数传递、类型检查和安全性检查等需要时间开销,会影响程序的运行效率。
为了解决上述问题,提出了使用友元的方案。友元是一种说明在类体内的非成员函数。为了与该类的成员函数加以区别,说明时前面加上关键字 friend
。友元不是成员函数,但它可以访问类中的私有成员。友元的作用在于提高程序的运行效率,但它破坏了类的封装性和隐藏性,使得非成员函数可以访问类中的私有成员。
友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类。下面分别讲述这两种友元。
5.6.1 友元函数
友元函数的特点是能够访问类中的私有成员和其他成员函数。友元函数在定义和调用上与普通函数一样。
示例代码
#include <iostream>
#include <cmath>
class Point {
public:
Point(double xx, double yy) : x(xx), y(yy) {}
void Getxy() const;
friend double Distance(const Point &a, const Point &b);
private:
double x, y;
};
void Point::Getxy() const {
std::cout << "(" << x << "," << y << ")" << std::endl;
}
double Distance(const Point &a, const Point &b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return std::sqrt(dx * dx + dy * dy);
}
int main() {
Point p1(3.0, 4.0), p2(6.0, 8.0);
p1.Getxy();
p2.Getxy();
double d = Distance(p1, p2);
std::cout << "Distance is " << d << std::endl;
return 0;
}
输出结果
(3.0,4.0)
(6.0,8.0)
Distance is 5
说明:该程序的 Point
类中声明了一个友元函数 Distance
,在说明时前面加上 friend
关键字,表示它是友元函数。友元函数可以访问类中的私有成员,通过对象引用访问 a.x
和 b.x
。调用友元函数与普通函数的调用一样,不需要对象引用。
另一个示例
#include <iostream>
class Time {
public:
Time(int new_hours, int new_minutes) : hours(new_hours), minutes(new_minutes) {}
friend void Time12(Time time);
friend void Time24(Time time);
private:
int hours, minutes;
};
void Time12(Time time) {
if (time.hours > 12) {
time.hours -= 12;
std::cout << "Time in 12-hour format: " << time.hours << ":" << time.minutes << " PM" << std::endl;
} else {
std::cout << "Time in 12-hour format: " << time.hours << ":" << time.minutes << " AM" << std::endl;
}
}
void Time24(Time time) {
std::cout << "Time in 24-hour format: " << time.hours << ":" << time.minutes << std::endl;
}
int main() {
Time Time1(20, 38), Time2(10, 45);
Time12(Time1);
Time24(Time1);
Time12(Time2);
Time24(Time2);
return 0;
}
输出结果
Time in 12-hour format: 8:38 PM
Time in 24-hour format: 20:38
Time in 12-hour format: 10:45 AM
Time in 24-hour format: 10:45
说明:该程序中有两个友元函数 Time12
和 Time24
,它们的定义和调用与普通函数一样,但可以访问 Time
类的私有成员 hours
和 minutes
。
5.6.2 友元类
友元除了可以是函数外,还可以是类。即一个类可以作为另一个类的友元。当一个类作为另一个类的友元时,这意味着这个类的所有成员函数都是另一个类的友元函数。例如,如果类 Y 是类 X 的友元类,则 Y 类的所有成员函数都是 X 类的友元函数,它们都可以访问 X 类中的所有成员。
示例代码
#include <iostream>
class X {
friend class Y;
public:
void Set(int i) { x = i; }
void Display() const { std::cout << "x = " << x << ", y = " << y << std::endl; }
private:
int x;
static int y;
};
int X::y = 1;
class Y {
public:
Y(int i, int j) { a.x = i; X::y = j; }
void Display() const;
private:
X a;
};
void Y::Display() const {
std::cout << "x = " << a.x << ", y = " << X::y << std::endl;
}
int main() {
X b;
b.Set(5);
b.Display();
Y c(6, 9);
c.Display();
b.Display();
return 0;
}
输出结果
x = 5, y = 1
x = 6, y = 9
x = 5, y = 9
说明:在该程序中,X
类中说明了 Y
类为友元类。因此,Y
类的成员函数可以访问 X
类的私有成员。在 Y
类中定义了一个静态成员 y
,通过 Y
类对象 c
改变了它的值,X
类的对象 b
也会反映出改变后的值。
总结
友元是为了提高程序效率而引入的,它允许非成员函数或类访问类的私有成员。友元函数和友元类在定义和使用上与普通函数和类没有太大区别,但它们破坏了类的封装性和隐藏性。友元应谨慎使用,避免滥用以保证代码的安全性和可维护性。