学习C++(5)运算符重载+赋值运算符重载

1.运算符重载

(1)运算符重载的概念

当运算符被用于类类型的对象时,C++允许通过运算符重载的形式指定特定的含义。C++规定类类型对象使用运算符时,必须转换成调用对应运算符重载,若没有对应的运算符重载,则会编译报错。

运算符重载是具有特殊函数名的函数,函数名由operator和要定义的运算符共同构成。

(2)运算符重载的写法

若运算符重载函数写成全局的,必然会碰到需要访问私有变量的问题,解决方法有:

①将私有成员改为公有(当然这不推荐)

②在对象里提供getxxx函数,代码如下:

cpp 复制代码
#include<iostream>
using namespace std;
class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    int GetYear() const
    {
        return _year;
    }
    int GetMonth() const
    {
        return _month;
    }
    int GetDay() const
    {
        return _day;
    }
private:
    int _year;
    int _month;
    int _day;
};
bool operator==(const Date& d1,const Date& d2)
{
    return d1.GetYear()==d2.GetYear()
    &&d1.GetMonth()==d2.GetMonth()
    &&d1.GetDay()==d2.GetDay();
}
int main()
{
    Date d1(2026,1,30);
    Date d2(2026,1,31);
    operator==(d1, d2);//显式调用
    d1==d2//隐式调用
    return 0;
}

③友元函数(在后续的内容中介绍,但这种方法同样不推荐)

若运算符重载函数写成成员函数(这是推荐的方法),代码如下:

cpp 复制代码
#include<iostream>
using namespace std;
class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    bool operator==(const Date& d)
    {
        return _year == d._year
               && _month == d._month
               && _day == d._day;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1(2026,1,30);
    Date d2(2026,1,31);
    d1.operator==(d2);//显式调用
    d1==d2//隐式调用
    return 0;
}
(3)运算符重载的特性

①重载运算符函数的参数个数和该运算符作用的运算对象数量一样多。

②运算符重载以后,其优先级和结合性与对应的内置类型运算符保持一致。

③不能通过连接语法中没有的符号来创建新的操作符,例如operator@。

④**++.* :: sizeof ?: .++**这五个运算符不能重载

⑤重载操作必须至少有一个类类型参数,不能通过运算符重载改变内置类型对象运算符原先的含义

⑥重载++运算符时,有前置++和后置++,C++规定,后置++重载时,增加一个int形参,跟前置++构成函数重载,方便区分,代码如下:

cpp 复制代码
#include<iostream>
using namespace std;
class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    Date& operator++()//前置++
    {
        //...
        return *this;
    }
    Date operator++(int)//后置++
    {
        Date tmp;
        //...
        return tmp;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1(2026,1,30);
    d1.operator++();//前置++显式调用
    ++d1//前置++隐式调用

    d1.operator++(0);//后置++显式调用
    d1++//后置++隐式调用
    return 0;
}

2.赋值运算符重载

赋值运算符重载是一个默认成员函数,用于完成两个已经存在的对象直接的拷贝赋值。

(1)赋值运算符重载的写法

赋值运算符重载是一个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成 const 当前类类型引用,可以提高效率,返回值也建议写成当前类类型引用,有返回值是为了支持连续赋值场景。代码如下:

cpp 复制代码
#include<iostream>
using namespace std;
class Date
{
public:
    Date(int year = 1, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
    Date& operator=(const Date& d)
    {
        if (this != &d)//要检查自己给自己赋值的情况
        {
            _year = d._year;
            _month = d._month;
            _day = d._day;
        }
        return *this;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1(2026,1,30);
    Date d2(2026,1,31);
    d1.operator=(d2);//显式调用
    d1=d2//隐式调用
    return 0;
}
(2)赋值运算符重载的特性

①没有显式实现时,编译器会自动生成一个默认赋值运算符重载,默认赋值运算符重载行为跟默认拷贝构造函数类似,对内置类型成员变量会完成值拷贝,对自定义类型成员变量会调用它的赋值重载函数。

②如果一个类显式实现了析构并释放资源,那么它就需要显式写赋值运算符重载,否则就不需要。

相关推荐
apple-mapping6 小时前
网络RTK定位学习总结
学习·网络rtk
楚山之石7 小时前
【Lancet Neurology 】多发性硬化认知障碍的 SDMT 筛查、灰质萎缩标志与认知康复循证路径
图像处理·人工智能·学习·mri
海海不掉头发8 小时前
CSDN博客_时序预测9模型复现对比
人工智能·学习·软件工程
蓝祁子yoky10 小时前
Qt学习日记 Day13:网络编程(一)-TCP基础与Socket实战
网络·qt·学习
架构师伯子明11 小时前
Ros 学习十三:IK Demo
学习
小羊没烦恼!11 小时前
关于分布式锁原理的一些学习与思考-redis分布式锁,zookeeper分布式锁
java·服务器·网络·学习·负载均衡
幼儿园蛋小黄12 小时前
从 STM32N647 的 AI 部署入门嵌入式:一条可落地的学习路线
人工智能·stm32·学习
Y.99913 小时前
电脑黑屏只有鼠标能动
学习
架构师伯子明13 小时前
FFmpeg C++学习(一):编译和demo
开发语言·c++·学习
叼烟扛炮14 小时前
[LangChain] 学习笔记(02):LCEL 链式调用与动态模型配置实战
服务器·开发语言·网络·笔记·学习·langchain