C++中友元的详解及其作用介绍

目录概述友元普通的友元函数友元成员函数友元类总结

概述

类的友元函数 (friend) 是定义在类外部, 但是有权限访问类的所有私有 (private) 成员和保护 (protected) 成员.

友元

我们先来复习一下公有成员和私有成员的概念:

公有成员 (public) : 在类外可以访问 私有成员 (private): 只有本类中的函数可以访问

友元 (friend) 可以访问与其有好友关系的类中的私有成员 (有限制的共享).

友元包括友元函数和友元类:

友元函数: 如果在本类以外的其他地方定义的函数, 在类体重用 friend 进行声明. 此函数就称为本类的有元函数, 友元函数可以访问这个类中的私有成员 友元类: 类 A 将另一个类 B 声明为其友元类, 友元类 B 中的所有函数都是 A 类的友元函数, 可以访问 A 类中的所有成员

普通的友元函数

Time 类:

#ifndef PROJECT2_TIME_H#define PROJECT2_TIME_Hclass Time {private:    int hour;    int minute;    int second;public:    Time();    Time(int, int, int);    friend void display(Time &);  // display是Time类的friend函数};#endif //PROJECT2_TIME_H

Time.cpp:

#include <iostream>#include "Time.h"using namespace std;Time::Time() : hour(0), minute(0), second(0) {}Time::Time(int h, int m, int s) : hour(h), minute(m), second(s) {}void display(Time &t) {// display不是Time类的成员函数, 但是可以引用Time中的私有成员    cout << t.hour << ":" << t.minute << ":" << t.second << endl;}

main:

#include "Time.h"#include <iostream>using namespace std;int main() {    Time t1(8, 8, 8);    display(t1);    return 0;}

友元成员函数

Time 类:

#ifndef PROJECT2_TIME_H#define PROJECT2_TIME_Hclass Date;  // 对Date类进行提前引用声明class Time {private:    int hour;    int minute;    int second;public:    Time();    Time(int, int, int);    void display(Date &d);};#endif //PROJECT2_TIME_H

Date 类:

#ifndef PROJECT2_DATE_H#define PROJECT2_DATE_H#include "Time.h"class Date {private:    int year, month, day;public:    Date(int, int, int);    friend void Time::display(Date &d);};#endif //PROJECT2_DATE_H

Time.cpp:

#include <iostream>#include "Time.h"#include "Date.h"using namespace std;Time::Time() : hour(0), minute(0), second(0) {}Time::Time(int h, int m, int s) : hour(h), minute(m), second(s) {}void Time::display(Date &d) {    cout << d.year << "年" << d.month << "月" << d.day << "日" <<endl;    cout << hour << ":" << minute << ":" << second << endl;}

main:

#include "Time.h"#include "Date.h"#include <iostream>using namespace std;int main() {    Time t1(8, 8, 8);    Date d1(2021, 5, 6);    t1.display(d1);    return 0;}

输出结果:

2021年5月6日8:8:8

我们可以发现 display 不是 Date 类的成员函数, 但是可以引用 Date 中的私有成员.

友元类

Time 类:

#ifndef PROJECT2_TIME_H#define PROJECT2_TIME_Hclass Date;  // 对Date类进行提前引用声明class Time {private:    int hour;    int minute;    int second;public:    Time();    Time(int, int, int);    void display(Date &d);};#endif //PROJECT2_TIME_H

Date 类:

#ifndef PROJECT2_DATE_H#define PROJECT2_DATE_H#include "Time.h"class Date {private:    int year, month, day;public:    Date(int, int, int);    friend class Time;  // 友元类};#endif //PROJECT2_DATE_H

总结 如果在本类以外的地方定义了一个函数在类体中用 friend 对其进行声明, 此函数就称为本类的友元函数 友元函数可以访问这个类中的私有成员 友元函数可以是不属于任何类的非成员函数, 也可以是其他类的成员函数 一个函数可以被多个类声明为 friend, 这样就可以引用多个类中的私有数据.

友元的性质:

友元的关系是单向的而不是双向的 友元的关系不能传递

友元的优缺点:

优点: 有助于数据共享, 提高程序效率 缺点: 破坏了封装原则, 不利于信息隐藏

我们在使用友元的时候, 应当时刻考虑友元的缺点. 如果能用公共成员函数解决就不必用友元.

到此这篇关于C++中友元的详解及其作用介绍的文章就介绍到这了,更多相关C++友元内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

当你能飞的时候就不要放弃飞

C++中友元的详解及其作用介绍

相关文章:

你感兴趣的文章:

标签云: