C++:运算符重载与类的赋值运算符重载函数

简介: C++:运算符重载与类的赋值运算符重载函数

章节知识架构

一.运算符重载

  1. 运算符重载的基本概念

    代码段1

2.关于运算符重载的重要语法细则

二.运算符重载在类中的使用

三.类的默认成员函数:=重载函数(赋值运算符重载)

1.自定义=重载函数

代码段2

2.编译器默认生成的=重载函数

四.前置++(--)和后置++(--)的重载

章节知识架构

一.运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数

  1. 运算符重载的基本概念

C++中定义运算符重载的关键字:operator

运算符重载本质上是函数,具有返回值类型,函数名字以及参数列表,其返回值类型和参数列表与普通的函数定义规则类似;
运算符重载函数的函数名命名规则:关键字operator + 需要重载的运算符符号(例如定义函数名operator>,表示>的运算符重载);
运算符重载函数的形参个数必须和被重载运算符的目数相同:单目操作符的重载函数有且只有一个形参,双目操作符的重载函数有且只有两个形参.
运算符重载函数必须有一个类类型参数
运算符重载函数首部的一般形式:返回值类型+operator运算符+(参数列表);
比如:

现在有一个记录日期的类Date,我们想比较两个日期类对象所记录的日期大小,因此设计一个运算符 '>'的重载函数。

函数返回类型定义为bool;bool为一个字节的整形,其只能为true(值1)或false(值为0)

函数名定义为: operator>

函数的形参表为:(const Date & date2 ,const Date & date2)

代码段1

include

using std::cout;
using std::cin;
using std::endl;

class Date 记录日期的类
{
public:

Date(int day = 0, int month = 0,int year = 0)          Date的带参构造函数
{
    _day = day;
    _month = month;
    _year = year;
}

int _day;
int _month;
int _year;

};

bool operator> (const Date& date1, const Date& date2) 将 > 运算符进行重载用于日期比较
{

if (date1._year > date2._year)
{
    return true;
}
else if (date1._year == date2._year && date1._month > date2._month)
{
    return true;
}
else if (date1._year == date2._year && date1._month == date2._month && date1._day >                 
date1._day)
{
    return true;
}
return false;

}

int main()
{

Date a(2021, 4, 23);
Date b(2022, 1, 27);

if (a > b)                      用运算符重载来比较两个日期对象
{
    cout << "Date a > Date b" << endl;
}
else
{
    cout << "Date a < Date b" << endl;
}
return 0;

}

C++编译其会根据运算符的操作数的类型来判断该运算符是否要调用重载以及调用哪种形式的重载,比如上述代码段主函数中的 a>b表达式:

2.关于运算符重载的重要语法细则
不能通过连接其他符号来创建新的操作符:比如operator@
作为类成员运算符重载函数时,其形参个数看起来比运算符目数少1,因为类成员函数的第一个参数为隐藏的this指针;关于this指针:http://t.csdn.cn/hncvq
赋值运算符重载(比如=,+=,-+,*=等等)只能作为类的成员函数不能重载成全局函数
'".* " "::" "sizeof" "?:" "." 注意以上5个运算符不能重载。
二.运算符重载在类中的使用
C++引入运算符重载是为了提高代码的可读性,允许我们构建复杂类对象之间的运算表达式。
运算符重载必须有一个类类型形参,因此在运算符重载函数中我们难免要访问类的成员变量
为了保证封装性,类的成员变量往往定义在类的私有域中。
所以为了保证类对象的封装性的同时可以让运算符重载函数直接访问到类的成员变量,大多数情况下我们往往把运算符重载函数定义为类的成员函数(方法)。
现在对代码段1进行优化:

把运算符重载函数定义为类的成员函数(方法)

运算符重载函数定义为类的成员函数时注意不要忽略this指针

include

using std::cout;
using std::cin;
using std::endl;

class Date //记录日期的类
{
public:

Date(int day = 0, int month = 0,int year = 0)
{
    _day = day;
    _month = month;
    _year = year;
}

bool operator> (const Date& date)       //将 > 运算符进行重载
{
    if (_year > date._year)
    {
        return true;
    }
    else if (_year == date._year &&_month > date._month)
    {
        return true;
    }
    else if (_year == date._year && _month == date._month && _day > date._day)
    {
        return true;
    }
    return false;
}

private:

int _day;
int _month;
int _year;

};

int main()
{

Date a(29, 4, 2020);
Date b(27, 4, 2020);

if (a > b)
{
    cout << "Date a > Date b" << endl;
}
else
{
    cout << "Date a < Date b" << endl;
}
return 0;

}

简单地观察一下 a>b表达式的汇编指令:

三.类的默认成员函数:=重载函数(赋值运算符重载)
类的默认成员函数中有一个赋值运算符=的重载:

如果我们没有在类中自定义一个赋值运算符=的重载,编译器会在类中自动生成一个默认的=运算符重载函数。

1.自定义=重载函数
赋值运算符(=)只能重载成类的成员函数不能重载成全局函数

原因:赋值运算符如果没有被我们定义,编译器会在类中(任何类中)生成一个默认的赋值运算符重载。此时若我们在类域外定义一个全局的赋值运算符重载,就会和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。
自定义=重载函数:

现在Date类中实现一个=重载函数:如果我们在类中自定义了=重载函数,编译器便不会在编译阶段自动生成其默认的赋值运算符(=)重载。

代码段2
using std::cout;
using std::cin;
using std::endl;

class Date 记录日期的类
{
public:

Date(int day = 0, int month = 0,int year = 0)
{
    _day = day;
    _month = month;
    _year = year;
}

bool operator> (const Date& date)       将 > 运算符进行重载
{
    if (_year > date._year)
    {
        return true;
    }
    else if (_year == date._year &&_month > date._month)
    {
        return true;
    }
    else if (_year == date._year && _month == date._month && _day > date._day)
    {
        return true;
    }
    return false;
}

Date& operator=(const Date& date)        将 = 运算符进行重载
{
    _day = date._day;
    _month = date._month;
    _year = date._year;
    return (*this);
}

private:

int _day;
int _month;
int _year;

};

int main()
{

Date a(29, 4, 2020);
Date b;
Date c;

c = b = a;                              用=的重载完成连续赋值

if (a > b)
{
    cout << "Date a > Date b" << endl;
}
else 
{
    cout << "Date a <= Date b" << endl;
}
return 0;

}

可以试着观察一下代码段中c=b=a的汇编指令:

注意:=重载函数使用引用传参和引用返回而不使用值传参和值返回是为了提高代码的运行效率(值传递需要拷贝出临时的类对象)

参见:http://t.csdn.cn/Rc5aI

       http://t.csdn.cn/9tLyK

2.编译器默认生成的=重载函数
编译器默认生成的=重载函数:

编译器默认生成的=运算符重载函数的功能和代码段2中自定义的=重载函数功能类似:

可以简单验证一下:

将代码段2中等号重载的代码段注释掉

using std::cout;
using std::cin;
using std::endl;

class Date //记录日期的类
{
public:

Date(int day = 0, int month = 0,int year = 0)
{
    _day = day;
    _month = month;
    _year = year;
}

bool operator> (const Date& date)       //将 > 运算符进行重载
{
    if (_year > date._year)
    {
        return true;
    }
    else if (_year == date._year &&_month > date._month)
    {
        return true;
    }
    else if (_year == date._year && _month == date._month && _day > date._day)
    {
        return true;
    }
    return false;
}

//Date& operator=(const Date& date)     //将等号重载的代码段注释掉
//{
//    if (this != &date)
//    {
//        _day = date._day;
//        _month = date._month;
//        _year = date._year;
//    }

//    return (*this);
//}

private:

int _day;
int _month;
int _year;

};

int main()
{

Date a(29, 4, 2020);
Date b;
Date c;

c = b = a;

if (a > b)
{
    cout << "Date a > Date b" << endl;
}
else 
{
    cout << "Date a <= Date b" << endl;
}
return 0;

}

如果,类中有类对象成员变量,该类的编译器默认生成的=重载函数中会去调用其成员类对象的赋值运算符重载。

比如:

using std::cout;
using std::cin;
using std::endl;

class subclass //定义一个被嵌套的类
{
public:

subclass(int a = 100)             定义subclass的构造函数
{
    _a = 100;
}
subclass& operator = (const subclass& date)       定义subclass的赋值运算符重载函数
{
    _a = date._a;
    cout << "hello" << endl;                      打印一下表示该函数被调用
    return (*this);
}

private:

int _a;

};

class Date //记录日期的类
{
public:

Date(int day = 0, int month = 0,int year = 0,int subclass=0)
{
    _day = day;
    _month = month;
    _year = year;
}

//bool operator> (const Date& date)       //将 > 运算符进行重载
//{
//    if (_year > date._year)
//    {
//        return true;
//    }
//    else if (_year == date._year &&_month > date._month)
//    {
//        return true;
//    }
//    else if (_year == date._year && _month == date._month && _day > date._day)
//    {
//        return true;
//    }
//    return false;
//}

//Date& operator=(const Date& date)     //将等号重载的代码段注释掉
//{
//    if (this != &date)
//    {
//        _day = date._day;
//        _month = date._month;
//        _year = date._year;
//    }

//    return (*this);
//}

private:

int _day;
int _month;
int _year;
subclass _subdate;

};

int main()
{

Date a(29, 4, 2020,100);
Date b;
Date c;

c = b = a;

//if (a > b)
//{
//    cout << "Date a > Date b" << endl;
//}
//else 
//{
//    cout << "Date a <= Date b" << endl;
//}
return 0;

}

可以通过汇编指令来观察这个过程:

需要注意的是:类似于拷贝构造函数,编译器在类中默认生成的赋值运算符重载是以逐个字节进行拷贝的方式完成类对象赋值的(这种拷贝方式称为浅拷贝)。
因此编译器默认生成的赋值运算符重载不能用于一些申请了额外内存资源类对象之间的拷贝赋值。(比如栈对象之间的拷贝赋值)
如果栈对象之间使用编译器默认生成的=重载函数,则会出现以下情形:

所以涉及到内存资源管理的类对象之间只能使用用户自定义的=重载完成深拷贝。

四.前置++(--)和后置++(--)的重载
针对Date类,我们来实现一下表示日期自增的++重载(日期增加一天)

using std::cout;
using std::cin;
using std::endl;

//class subclass //定义一个被嵌套的类
//{
//public:
// subclass(int a = 100) //定义subclass的构造函数
// {
// _a = 100;
// }
// subclass& operator = (const subclass& date) //定义subclass的赋值运算符重载函数
// {
// _a = date._a;
// cout << "hello" << endl; //打印一下表示该函数被调用
// return (*this);
// }
//private:
//
// int _a;
//};

class Date //记录日期的类
{
public:

Date(int day = 0, int month = 0, int year = 0)   //Date的构造函数
{
    _day = day;
    _month = month;
    _year = year;
}

//bool operator> (const Date& date)       //将 > 运算符进行重载
//{
//    if (_year > date._year)
//    {
//        return true;
//    }
//    else if (_year == date._year &&_month > date._month)
//    {
//        return true;
//    }
//    else if (_year == date._year && _month == date._month && _day > date._day)
//    {
//        return true;
//    }
//    return false;
//}

//Date& operator=(const Date& date)     
//{
//    if (this != &date)
//    {
//        _day = date._day;
//        _month = date._month;
//        _year = date._year;
//    }

//    return (*this);
//}
Date& operator++()             //实现日期类的前置++
{
    _day++;
    return (*this);
}

Date operator++(int)           //实现日期类的后置++
{
    Date tem(*this);
    _day++;
    return tem;
}

void Print()                    //类对象的日期打印函数
{
    cout << _year << ' ' << _month << ' ' << _day << ' ' << endl;
}

private:

int _day;
int _month;
int _year;

};

int main()
{

Date a(5, 4, 2020);
a.Print();
Date ret1(a++);
ret1.Print();
Date ret2(++a);
ret2.Print();
return 0;

}

代码图解:

注意一个语法细则:

前置--和后置--的重载和++类似。

相关文章
|
2天前
|
安全 编译器 程序员
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
13 2
|
2天前
|
存储 C++
【C++篇】C++类和对象实践篇——从零带你实现日期类的超详细指南
【C++篇】C++类和对象实践篇——从零带你实现日期类的超详细指南
12 2
【C++篇】C++类和对象实践篇——从零带你实现日期类的超详细指南
|
1天前
|
编译器 C语言 C++
C++入门3——类与对象2-2(类的6个默认成员函数)
C++入门3——类与对象2-2(类的6个默认成员函数)
13 3
|
2天前
|
安全 C语言 C++
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
23 4
|
1天前
|
编译器 C语言 C++
详解C/C++动态内存函数(malloc、free、calloc、realloc)
详解C/C++动态内存函数(malloc、free、calloc、realloc)
8 1
|
1天前
|
存储 编译器 C++
C++入门3——类与对象2-1(类的6个默认成员函数)
C++入门3——类与对象2-1(类的6个默认成员函数)
10 1
|
1天前
|
编译器 C语言 C++
C++入门6——模板(泛型编程、函数模板、类模板)
C++入门6——模板(泛型编程、函数模板、类模板)
11 0
C++入门6——模板(泛型编程、函数模板、类模板)
|
2天前
|
存储 编译器 程序员
【C++篇】手撕 C++ string 类:从零实现到深入剖析的模拟之路
【C++篇】手撕 C++ string 类:从零实现到深入剖析的模拟之路
23 2
|
3天前
|
存储 编译器 C++
【C++】深入探索类和对象:初始化列表及其static成员与友元(一)
【C++】深入探索类和对象:初始化列表及其static成员与友元
|
2天前
|
存储 编译器 C语言
C++类与对象深度解析(一):从抽象到实践的全面入门指南
C++类与对象深度解析(一):从抽象到实践的全面入门指南
20 8