C++的运算符重载介绍

所谓重载,就是赋予新的含义。函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作。运算符重载(Operator Overloading) 也是一个道理,同一个运算符可以有不同的功能。

实际上,我们已经在不知不觉中使用了运算符重载。例如,+号可以对不同类型(int、float 等)的数据进行加法操作;<<既是位移运算符,又可以配合 cout 向控制台输出数据。

C++ 本身已经对这些运算符进行了重载。C++ 也允许程序员自己重载运算符,这给我们带来了很大的便利。

下面的代码定义了一个复数类,通过运算符重载,可以用+号实现复数的加法运算:

arduino 复制代码
    #include <iostream>
    using namespace std;
    class complex{
    public:
        complex();
        complex(double real, double imag);
    public:
        //声明运算符重载
        complex operator+(const complex &A) const;
        void display() const;
    private:
        double m_real;  //实部
        double m_imag;  //虚部
    };
    complex::complex(): m_real(0.0), m_imag(0.0){ }
    complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
    //实现运算符重载
    complex complex::operator+(const complex &A) const{
        complex B;
        B.m_real = this->m_real + A.m_real;
        B.m_imag = this->m_imag + A.m_imag;
        return B;
    }
    void complex::display() const{
        cout<<m_real<<" + "<<m_imag<<"i"<<endl;
    }
    int main(){
        complex c1(4.3, 5.8);
        complex c2(2.4, 3.7);
        complex c3;
        c3 = c1 + c2;
        c3.display();
        return 0;
    }

运行结果:6.7 + 9.5i

本例中义了一个复数类 complex,m_real 表示实部,m_imag 表示虚部,第 10 行声明了运算符重载,第 21 行进行了实现(定义)。

认真观察这两行代码,可以发现运算符重载的形式与函数非常类似。运算符重载其实就是定义一个函数,在函数体内实现想要的功能,当用到该运算符时,编译器会自动调用这个函数。

也就是说,运算符重载是通过函数实现的,它本质上是函数重载。运算符重载的格式为:

arduino 复制代码
返回值类型 operator 运算符名称 (形参表列){
    //TODO:
}

operator是关键字,专门用于定义重载运算符的函数。我们可以将operator 运算符名称这一部分看做函数名,对于上面的代码,函数名就是operator+。

运算符重载函数除了函数名有特定的格式,其它地方和普通函数并没有区别。

上面的例子中,我们在 complex 类中重载了运算符+,该重载只对 complex 对象有效。当执行c3 = c1 + c2;语句时,编译器检测到+号左边(+号具有左结合性,所以先检测左边)是一个 complex 对象,就会调用成员函数operator+(),也就是转换为下面的形式:

c3 = c1.operator+(c2);

c1 是要调用函数的对象,c2 是函数的实参。上面的运算符重载还可以有更加简练的定义形式:

arduino 复制代码
    complex complex::operator+(const complex &A)const{
        return complex(this->m_real + A.m_real, this->m_imag + A.m_imag);
    }

return 语句中的complex(this->m_real + A.m_real, this->m_imag + A.m_imag)会创建一个临时对象,这个对象没有名称,是一个匿名对象。在创建临时对象过程中调用构造函数,return 语句将该临时对象作为函数返回值。

在全局范围内重载运算符

运算符重载函数不仅可以作为类的成员函数,还可以作为全局函数。更改上面的代码,在全局范围内重载+,实现复数的加法运算:

arduino 复制代码
    #include <iostream>
    using namespace std;
    class complex{
    public:
        complex();
        complex(double real, double imag);
    public:
        void display() const;
        //声明为友元函数
        friend complex operator+(const complex &A, const complex &B);
    private:
        double m_real;
        double m_imag;
    };
    complex operator+(const complex &A, const complex &B);
    complex::complex(): m_real(0.0), m_imag(0.0){ }
    complex::complex(double real, double imag): m_real(real), m_imag(imag){ }
    void complex::display() const{
        cout<<m_real<<" + "<<m_imag<<"i"<<endl;
    }
    //在全局范围内重载+
    complex operator+(const complex &A, const complex &B){
        complex C;
        C.m_real = A.m_real + B.m_real;
        C.m_imag = A.m_imag + B.m_imag;
        return C;
    }
    int main(){
        complex c1(4.3, 5.8);
        complex c2(2.4, 3.7);
        complex c3;
        c3 = c1 + c2;
        c3.display();
        return 0;
    }

运算符重载函数不是 complex 类的成员函数,但是却用到了 complex 类的 private 成员变量,所以必须在 complex 类中将该函数声明为友元函数。

当执行c3 = c1 + c2;语句时,编译器检测到+号两边都是 complex 对象,就会转换为类似下面的函数调用:

c3 = operator+(c1, c2);

小结

虽然运算符重载所实现的功能完全可以用函数替代,但运算符重载使得程序的书写更加人性化,易于阅读。运算符被重载后,原有的功能仍然保留,没有丧失或改变。通过运算符重载,扩大了C++已有运算符的功能,使之能用于对象。

相关推荐
AllData公司负责人14 分钟前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
武子康1 小时前
火系被选七成,就应该削弱吗?
人工智能·算法·机器学习
夜不会漫长2 小时前
C++:C++11特性
开发语言·c++·算法
Dawson Zhu2 小时前
【多平台适配⑤】Benchmark方法论:如何构建公平可信的跨平台性能评测体系
人工智能·语言模型·架构·aigc·agi
秋天的怀念_David2 小时前
算法一千零一夜 · 第三十七夜 · 类型的过路费
java·程序人生·算法
无妄无望4 小时前
Always-on Agent 架构精读:如何让 Agent 长期运行而不失控
架构·大语言模型·claude·ai agent·agent harness
xckkcxxck5 小时前
Kaggle S6E10 冠军方案精读(二):245 个模型的合唱团,和一个只会“线性指挥“的老师
算法
云上工程笔记5 小时前
大模型平台私有化部署怎么选?2026年合规要求、运维难度与架构成本评测
运维·架构
饮水桶_T5 小时前
Git笔记
开发语言·笔记·算法
指掀涛澜天下惊6 小时前
强化学习进阶篇十四 DPO算法
深度学习·算法·机器学习·sft·预训练·dpo