大家好,我们今天来继续学习c++的类和对象,今天我们来着重学习一下c++中的运算符重载这一部分的知识。那话不多说,这就开始我们今天的学习吧。
目录
1. 运算符重载
我们知道,内置类型变量可以进行 + - * / 等运算符的修饰,那么自定义类型的对象可不可以使用这些运算符呢?这就是运算符重载。运算符重载就是一个函数来实现对自定义类型对象的运算符的使用。
运算符重载是具有特名字的函数,他的名字是由operator和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体。
需要注意的是,我们不能通过运算符重载来创造一个新的运算符,而且重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。
并且参数列表里至少有一个是自定义类型的变量,否则运算符重载就改变了内置类型对象的含义。
1.1 一个简单的运算符重载示例
那么接下来,我们就来自己试着实现一个运算符重载的示例:
比如说我们在日期类中想知道两个日期是否相等,可以这样写:
bool operator==(const Date& a,const Date& b)
{
return a._year == b._year && a._month == b._month && a._day == b._day;
}
但是此时看着是挺对的,但是其实是有错误的,因为全局函数无法直接访问类的私有成员,那么为了解决这个问题,有以下几个解决方法:
- 通过getyear ,getmonth 这样的函数来获取日期
- 将私有成员放在共有里
- 将运算符重载函数重载成类的成员函数
- 将运算符重载函数写成友元函数
由于友元函数还没有学到,所以这里最好的办法就是将运算符重载函数重载成成员函数了,那么我们看看这样该怎么写:
bool Date::operator==(const Date& b)
{
return _year == b._year && _month == b._month && _day == b._day;
}
因为成员函数的第一个参数是隐含的this指针,所以我们只需要写一个参数就行了。
那接下来,我们再来看两个特殊的运算符重载
1.2 前置++和后置++重载
因为运算符重载的函数名是operator后面接上重载的运算符,但由于前置++和后置++的函数名相同,此时应该怎么做区分呢?
c++规定,后置++的参数列表中多一个int类型的参数,由于区分它和前置++;
Date operator++(int x)
{
Date tmp = *this;
*this + = 1;
return tmp;
}
Date& operator++()
{
*this + = 1;
return *this;
}
该地方的*this +=1处也涉及到了+=的运算符重载,但是我还没有写出,大家先看一下进行理解,对于后置++,因为是该语句执行结束后才进行的自增,所以应该返回原始值,而对于前置++,返回的是自增后的值。
1.3 位移运算符<<和>>的重载
这里提到的位移运算符,其作用不是位运算,而是c++中的输入输出使用到的<<和>>,我们想要自定义输入输出类的成员变量,就要对这两个运算符进行重载,那我们来看一看要怎么写:
ostream& operator<<(ostream out)
{
out << _year << _month << _day;
}
istream& operator>>(istream in)
{
in >> _year >> _month >> _day;
}
这里的参数out和in分别是输出和输入流的变量,返回值是他们的类型,为了能狗进行连续输入输出,将io流的变量作为返回值,返回之后作为下一次输入输出的左值。但由于成员函数第一个参数是隐含的this指针,且运算符重载函数默认第一个参数对应运算符的左值,第二个参数对应右值,所以在该情况下调用应该是这样的:
d1 >> cin;
d1.operator>>(cin);
d1 << cout;
d1.operator<<(cout);
这样看起来是不是与我们正常的输入输出不一样,所以为了改善,我们应该写成全局函数来改变参数顺序,但此时又涉及到了之前的问题,此时只能将该重载函数写成友元函数,因为友元函数可以访问私有成员。
2. 赋值运算符的重载
- 赋值运算符重载是⼀个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成const 当前类类型引⽤,否则会传值传参会有拷⻉
- 有返回值,且建议写成当前类类型引⽤,引⽤返回可以提⾼效率,有返回值⽬的是为了⽀持连续赋值场景。
- 没有显式实现时,编译器会⾃动⽣成⼀个默认赋值运算符重载,默认赋值运算符重载⾏为跟默认构造函数类似,对内置类型成员变量会完成值拷⻉/浅拷⻉(⼀个字节⼀个字节的拷⻉),对⾃定义类型成员变量会调⽤他的拷⻉构造。
- 像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器⾃动⽣成的赋值运算符重载就可以完成需要的拷⻉,所以不需要我们显⽰实现赋值运算符重载。像Stack这样的类,虽然也都是内置类型,但是_a指向了资源,编译器⾃动⽣成的赋值运算符重载完成的值拷⻉/浅拷⻉不符合我们的需求,所以需要我们⾃⼰实现深拷⻉(对指向的资源也进⾏拷⻉)。像MyQueue这样的类型内部主要是⾃定义类型Stack成员,编译器⾃动⽣成的赋值运算符重载会调⽤Stack的赋值运算符重载,也不需要我们显⽰实现MyQueue的赋值运算符重载。这⾥还有⼀个⼩技巧,如果⼀个类显⽰实现了析构并释放资源,那么他就需要显⽰写赋值运算符重载,否则就不需要。
现在让我们写一下这个赋值运算符重载函数:
Date& operator=(const Date& d1)
{
_year = d._year;
_month = d._month;
_day = d._day;return *this;
}
但要注意的是赋值重载完成两个已经存在的对象直接的拷⻉赋值,⽽拷⻉构造⽤于⼀个对象拷⻉初始化给另⼀个要创建的对象。
下面我们来讲运算符重载这里最后一部分,取地址运算符重载
3. 取地址运算符重载
3.1 const成员函数
void Print() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
在这个打印日期的函数中,不涉及到成员变量的修改,此时我们就可以对该函数进行修饰,那么在其声明末尾加上const,就完成了对它的修饰,普通的对象可以调用被修饰的成员函数,但是被修饰的对象无法调用普通的成员函数。
3.2 取地址运算符重载
Date* operator&()
{
return this;
// return nullptr;
}
const Date* operator&()const
{
return this;
// return nullptr;
}
我们可以自己定义返回的地址来使程序出错,但是一般情况下这个运算符重载不需要我们自己显式定义。
学完了运算符重载,下面进行一项实战演练:实现日期类
4. 日期类的实现
在日期类中,由于没有涉及到资源的申请,所以我们不用显式定义析构函数,也就不用写拷贝构造函数。日期类就是我们前面的学习的知识的一个整合,这里就不多详细介绍了:
Date.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream out, const Date& d);
friend istream& operator>>(istream in, const Date& d);
public:
Date(int year,int month,int day)
{
_year = year;
_month = month;
_day = day;
if (!d.CheckDate())
{
cout << "⽇期⾮法" << endl;
}
}
Date(const Date& d1)
{
_year = d1._year;
_month = d1._month;
_day = d1._day;
}
void Print()const
{
cout << _year << _month << _day;
}
Date operator+(int day)const;
Date& operator+=(int day);
Date operator-(int day)const;
Date& operator-=(int day);
bool CheckDate();
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator==(const Date& d) const;
bool operator!=(const Date& d) const;
int operator-(const date& d)const;
Date operator++(int)const;
Date& operator++();
Date operator--(int)const;
Date& operator--();
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream out, const Date& d);
istream& operator>>(istream in, const Date& d);
Date.cpp
#include"Date.h"
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
return a[month];
}
Date Date:: operator+(int day) const
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date& Date:: operator+=(int day)
{
if (day < 0)
{
return *this-=day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date:: operator-(int day)const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date& Date:: operator-=(int day)
{
if (day < 0)
{
return *this += day;
}
_day -= day;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
bool Date::CheckDate()
{
if (_month < 1 || _month>12 || _day<1 || day>GetMonthDay(_year, _month))
{
return false;
}
return true;
}
bool Date::operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if(_year==d._year) {
if (_month < d._year)
{
return true;
}
else if (_month == d._month)
{
if (_day < d._day)
{
return true;
}
}
}
return false;
}
bool Date::operator<=(const Date& d) const
{
return (*this < d) || (*this == d);
}
bool Date:: operator> (const Date& d) const
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year) {
if (_month > d._year)
{
return true;
}
else if (_month == d._month)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
bool Date:: operator>= (const Date & d) const
{
return (*this > d) || (*this == d);
}
bool Date:: operator== (const Date & d) const
{
return (_year == d._year) && (_month == d._month) && (_day == d._day);
}
bool Date:: operator!= (const Date & d) const
{
return !(*this == d);
}
int Date::operator-(const Date& d)const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min < max)
{
++min;
++n;
}
return n * flag;
}
Date operator++(int)const
{
Date tmp=*this;
*this+=1;
return tmp;
}
Date& operator++()
{
*this += 1;
return *this;
}
Date operator--(int)const
{
Date tmp = *this;
*this -= 1;
return tmp;
}
Date& operator--()
{
*this -= 1;
return *this;
}
ostream& operator<<(ostream out, const Date& d)
{
out << d._year << d._month << d._day;
return out;
}
istream& operator>>(istream in, const Date& d)
{
in >> d._year >> d._month >> d._day;
if (!d.CheckDate())
{
cout << "⽇期⾮法" << endl;
}
return in;
}
那么关于运算符重载这一部分的知识就结束了,我们下次再见!