C++ 知识点17 友元

C++ 友元(friend)


一、友元是什么

友元 :用 friend 关键字,让外部函数 / 其他类

可以直接访问本类的 private、protected 私有成员。

核心:

  • 破坏封装,但能简化运算符重载、跨类访问

  • 友元不是类的成员,没有 this 指针


二、友元函数(全局函数做友元)

语法

类里面声明:

cpp 复制代码
friend 返回值 函数名(参数);

完整代码示例

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
class Person
{
    // 私有成员
    string name;
    int age;
​
    // 声明全局函数为友元
    friend void showPerson(const Person& p);
​
public:
    Person(string n, int a) : name(n), age(a) {}
};
​
// 全局普通函数
void showPerson(const Person& p)
{
    // 是友元,直接访问 private 成员
    cout << "姓名:" << p.name << " 年龄:" << p.age << endl;
}
​
int main()
{
    Person p("张三", 18);
    showPerson(p);
    return 0;
}

输出:

cpp 复制代码
姓名:张三 年龄:18

重点:

  • showPerson 不是类成员,是全局函数

  • 因为加了 friend,能直接读私有成员


三、用友元重载运算符(必考)

前面讲运算符重载时:

左操作数不是本类对象,必须用友元重载。

示例:重载 +,实现 int + 对象

cpp 复制代码
#include <iostream>
using namespace std;
​
class Point
{
private:
    int x, y;
public:
    Point(int a = 0, int b = 0) : x(a), y(b) {}
​
    // 声明友元运算符重载
    friend Point operator+(int num, const Point& p);
    friend void print(const Point& p);
};
​
// 全局实现
Point operator+(int num, const Point& p)
{
    return Point(num + p.x, num + p.y);
}
​
void print(const Point& p)
{
    cout << p.x << " , " << p.y << endl;
}
​
int main()
{
    Point p(10, 20);
    Point res = 5 + p;   // 左操作数是int,只能用友元
    print(res);
    return 0;
}

四、友元类(整个类做友元)

语法:

cpp 复制代码
friend class 类名;

含义:

  • B 是 A 的友元类

  • B 的所有成员函数都可以访问 A 的私有成员

代码演示

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
// 前置声明
class Person;
​
// 教师类
class Teacher
{
public:
    void getInfo(Person& p);
};
​
class Person
{
private:
    string name = "李四";
    int age = 20;
​
    // 声明 Teacher 为友元类
    friend class Teacher;
};
​
void Teacher::getInfo(Person& p)
{
    // 友元类,直接访问私有
    cout << p.name << " " << p.age << endl;
}
​
int main()
{
    Person p;
    Teacher t;
    t.getInfo(p);
    return 0;
}

友元类特点

  1. 友元关系单向:A 是 B 友元 ≠ B 是 A 友元

  2. 友元不继承:父类友元不会传给子类

  3. 不传递:A 友元 B,B 友元 C ≠ A 友元 C


五、友元成员函数(只让对方一个成员函数做友元)

只指定另一个类里的某一个函数为本类友元,不是整个类。

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
// 前置声明
class Student;
​
class School
{
public:
    void showStu(Student& s);
};
​
class Student
{
private:
    string id = "2026001";
​
    // 只把 School::showStu 设为友元
    friend void School::showStu(Student& s);
};
​
void School::showStu(Student& s)
{
    cout << "学号:" << s.id << endl;
}
​
int main()
{
    Student s;
    School sc;
    sc.showStu(s);
    return 0;
}

六、友元关键规则(考试必背)

  1. friend 声明在类内,不受 public/private 位置影响;

  2. 友元不是类的成员函数,没有 this 指针;

  3. 友元可以访问类所有权限成员(private/protected/public);

  4. 友元关系单向、不继承、不传递;

  5. 运算符重载中,左操作数不是本类时,必须用友元;

  6. 尽量少用友元,会破坏封装性。


七、三种友元一句话区分

  1. 友元函数:全局函数做友元

  2. 友元类:整个类所有函数都有权访问

  3. 友元成员函数:只允许对方某一个成员函数访问

相关推荐
小黄人软件几秒前
粘贴助手C++版 支持 Windows 7 / 10 / 11 不会出现缺少api-ms-win-core-path-11-1-0.dll
开发语言·c++·windows
有毒的教程11 分钟前
Dev-C++ 避免爆栈(Stack Overflow)完整方案
c++·算法·深度优先
半亩码田11 分钟前
C#转Python第4.7篇:logging:Python 的标准日志库
开发语言·python·c#
程序员老陆12 分钟前
从汇编层面理解 C++ 多线程
开发语言·c++·程序设计
谢亮_vipxieliang20 分钟前
深入理解 JavaScript 数据类型与类型转换
开发语言·javascript·ecmascript
在所不辞兄28 分钟前
刚性问题误差容限设置技巧
深度学习·神经网络·算法·机器学习
IOT-Power28 分钟前
模板方法 + 策略模式
开发语言·c++
啊啊啊啊啊!!!!31 分钟前
unordered_map和unordered_set的使用
算法
EatFan33 分钟前
2026 Rust 后端技术栈选型:Axum 0.8 + Tokio + SQLx 全链路怎么搭
开发语言·后端·rust·tokio·serde·axum·sqlx
外收内放33 分钟前
Python基础语法练习题(59-原始版本与优化版本)
开发语言·python