在阅读此篇文章之前,请先阅读博主之前的文章:
C++类和对象第一关-CSDN博客
C++类和对象——第二关-CSDN博客
以便更好的理解本文章。
目录
运算符重载
(一)运算符重载
(二)赋值类运算符函数的重载:
定义:
赋值运算符重载的特点:
(三)日期类的实现
(四)const成员函数
(五)取地址运算符重载
运算符重载
(一)运算符重载
• 当运算符被⽤于类类型的对象时,C++语⾔允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使⽤运算符时,必须转换成调⽤对应运算符重载,若没有对应的运算符重载,则会编译报错。
• 运算符重载是具有特殊名字的函数,他的名字是由operator和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体。
就把他当做普通的函数就好,只不过函数名不一样罢了。
• 重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。
• 如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数⽐运算对象少⼀个。
• 运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。
• 不能通过连接语法中没有的符号来创建新的操作符:⽐如operator@。
• .* :: sizeof ?: . 注意以上5个运算符不能重载。(选择题⾥⾯常考,⼤家要记⼀
下)
• 重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: intoperator+(int x, int y)
• ⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义,⽐如Date类重载operator-就有意义,但是重载operator+就没有意义。
• 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。
• 重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第⼀个形参位置,第⼀个形参位置是左侧运算对象,调⽤时就变成了对象<<cout,不符合使⽤习惯和可读性。重载为全局函数把ostream/istream放到第⼀个形参位置就可以了,第⼆个形参位置
当类类型对象。
(二)赋值类运算符函数的重载:
定义:
赋值运算符重载是⼀个默认成员函数,⽤于完成两个已经存在的对象直接的拷⻉赋值,这⾥要注意跟拷⻉构造区分,拷⻉构造⽤于⼀个对象拷⻉初始化给另⼀个要创建的对象
赋值运算符重载的特点:
1. 赋值运算符重载是⼀个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成const当前类类型引⽤,否则会传值传参会有拷⻉
2. 有返回值,且建议写成当前类类型引⽤,引⽤返回可以提⾼效率,有返回值⽬的是为了⽀持连续赋值场景。
3. 没有显式实现时,编译器会⾃动⽣成⼀个默认赋值运算符重载,默认赋值运算符重载⾏为跟默认拷⻉构造函数类似,对内置类型成员变量会完成值拷⻉/浅拷⻉(⼀个字节⼀个字节的拷⻉),对⾃定义类型成员变量会调⽤他的赋值重载函数。
4. 像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器⾃动⽣成的赋值运算符重载就可以完成需要的拷⻉,所以不需要我们显⽰实现赋值运算符重载。像Stack这样的类,虽然也都是内置类型,但是_a指向了资源,编译器⾃动⽣成的赋值运算符重载完成的值拷⻉/浅拷⻉不符合我们的需求,所以需要我们⾃⼰实现深拷⻉(对指向的资源也进⾏拷⻉)。像MyQueue这样的类型内部主要是⾃定义类型Stack成员,编译器⾃动⽣成的赋值运算符重载会调⽤Stack的赋值运算符重载,也不需要我们显⽰实现MyQueue的赋值运算符重载。这⾥还有⼀个⼩技巧,如果⼀个类显⽰实现了析构并释放资源,那么他就需要显⽰写赋值运算符重载,否则就不需要。
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
// 赋值运算符重载
class Date {
public:
Date(int day=1,int month=1,int year=2001)
{
_day = day;
_month = month;
_year = year;
}
~Date()
{}
// 赋值运算符重载:用于两个已经存在的对象之间的拷贝
Date& operator=(const Date& d)// 返回引用能够减少拷贝
{
// 检查自己拷贝自身的情况,
if (this != &d)// 检查两者地址是否一致
{
// 如果地址不一致
_day = d._day;
_month = d._month;
_year = d._year;
}
return *this;// 返回当前被拷贝的对象,这样能够支持连续赋值
}
void print()
{
cout <<this->_year << " /" << this->_month << " /" << this->_day << endl;
}
private:
int _year;
int _month;
int _day;
};
void Test()
{
Date d1(2001,9,18);
Date d2(2002,9,18);
Date d3(2003,9,18);
cout << "未赋值拷贝" << endl;
d1.print();
d2.print();
d3.print();
d1 = d2 = d3;
cout << "赋值拷贝" << endl;
d1.print();
d2.print();
d3.print();
}
int main()
{
Test();
return 0;
}
其实这个赋值拷贝除了第一点和拷贝构造不一样,还支持连续赋值拷贝之外其他的都和拷贝构造函数是一样的。
(三)日期类的实现
根据以上运算符重载的所有特性。我们来实现一个日期类来详细看一下。
日期类的声明:Date.h
#pragma once
#pragma once
#include<iostream>
#include<cassert>
using namespace std;
class Date
{
public:
// 友元声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month =#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
// 友元声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 析构函数
// ~Date();
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day);
// 日期-天数
Date operator-(int day);
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
bool operator>(const Date& d);
// ==运算符重载
bool operator==(const Date& d);
// >=运算符重载
bool operator >= (const Date& d);
// <运算符重载
bool operator < (const Date& d);
// <=运算符重载
bool operator <= (const Date& d);
// !=运算符重载
bool operator != (const Date& d);
// 日期-日期 返回天数
int operator-(const Date& d);
void Print()
{
cout << _year << '/' << _month << '/' << _day << endl;
}
// 日期检查
bool CheckDate()
{
if (_year < 0 || _month <= 0 || _month > 12 || _day <= 0 || _day > 31)
{
return false;
}
return true;
}
private:
int _year;
int _month;
int _day;
};
// 流插入 << 运算符重载
ostream& operator<<(ostream& out, const Date& d);
// 流提取 >> 运算符重载
istream& operator>>(istream& in, Date& d); 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 析构函数
// ~Date();
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day);
// 日期-天数
Date operator-(int day);
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
bool operator>(const Date& d);
// ==运算符重载
bool operator==(const Date& d);
// >=运算符重载
bool operator >= (const Date& d);
// <运算符重载
bool operator < (const Date& d);
// <=运算符重载
bool operator <= (const Date& d);
// !=运算符重载
bool operator != (const Date& d);
// 日期-日期 返回天数
int operator-(const Date& d);
void Print()
{
cout << _year << '/' << _month << '/' << _day << endl;
}
// 日期检查
bool CheckDate()
{
if (_year < 0 || _month <= 0 || _month > 12 || _day <= 0 || _day > 31)
{
return false;
}
return true;
}
private:
int _year;
int _month;
int _day;
};
// 流插入 << 运算符重载
ostream& operator<<(ostream& out, const Date& d);
// 流提取 >> 运算符重载
istream& operator>>(istream& in, Date& d);
看到friend关键字蒙圈的话请移至下一章节
日期类运算符函数重载的逻辑实现:Date.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
assert(_month > 0 && _month < 13);
int monthdayarry[] = { -1, 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31 }; // 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;
}
else
{
return monthdayarry[month];
}
}
// 日期+=天数
Date& Date::operator+=(int day)
{
assert(_month > 0 && _month < 13);
// 日期为负数
if (day < 0)
{
*this -= day;
}
// 日期为正数
_day += day;
while (_day > GetMonthDay( _year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month > 12)
{
++_year;
_month = 1;
}
// _day -= GetMonthDay(_year, _month);千万记住啊!!算法的步骤不能乱啊,乱了就出问题了。
}
return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
assert(_month > 0 && _month < 13);
Date tmp = *this;
tmp += day;
return tmp;
}
// 日期-天数
Date Date::operator-(int day)
{
assert(_month > 0 && _month < 13);
Date tmp = *this;
tmp -= day;
return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
assert(_month > 0 && _month < 13);
_day -= day;
while (_day < 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
// 因为是当前月减的,要加就要加下一个月的
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 前置++
Date& Date::operator++()
{
assert(_month > 0 && _month < 13);
return *this += 1;
}
// 后置++
Date Date::operator++(int)
{
assert(_month > 0 && _month < 13);
Date tmp = *this;
return tmp += 1;
}
// 前置--
Date& Date::operator--()
{
assert(_month > 0 && _month < 13);
return *this -= 1;
}
// 后置--
Date Date::operator--(int)
{
assert(_month > 0 && _month < 13);
Date tmp = *this;
return tmp -= 1;
}
// >运算符重载
bool Date::operator>(const Date& d)
{
assert(_month > 0 && _month < 13);
if (_year < d._year)
{
return false;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return false;
}
else if (_month == d._month)
{
if (_day < d._day)
{
return false;
}
}
}
return true;
// 为什么不可以这样写??
//return _day < d._year
// && _month < d._month
// && _year < d._year;
}
// ==运算符重载
bool Date::operator==(const Date& d)
{
assert(_month > 0 && _month < 13);
return _day == d._year
&& _month == d._month
&& _year == d._year;
}
// >=运算符重载
bool Date::operator >= (const Date& d)
{
return *this > d || *this == d;
}
// <运算符重载
bool Date::operator < (const Date& d)
{
return !(*this >= d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
return *this < d || *this == d;
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
assert(_month > 0 && _month < 13);
int n = 0;
Date min = *this;
Date max = d;
int flag = 1;
if (*this > d)
{
max = *this;
min = d;
flag = -1;
}
while (min <= max)
{
++min;
++n;
}
return n * flag;
}
// 流插入 << 运算符重载
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "/" << d._month << "/" << d._day << endl;
return out;
}
// 流提取 >> 运算符重载
istream& operator>>(istream& in, Date& d)
{
do
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
} while (!d.CheckDate());
return in;
}
ps:写代码的时候折磨自己很久的一个毛病:传参的实参和形参位置不一样导致出现了bug
测试函数Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
// 测试日期+天数和日期 +=天数
void Test01()
{
Date d1(2001, 6, 18);
d1.Print();
Date d2(d1 + 50);
d2.Print();
}
// 测试-和-=
void Test02()
{
Date d1(2001, 6, 18);
//d1.Print();
//d1 -= 40;
//d1.Print();
Date d2(d1 - 50);
d2.Print();
}
// 测试++和--
void Test03()
{
//Date d1(2001, 9, 18);
//d1.Print();
//d1++;
//d1.Print();
//Date d2(2002, 3, 28);
//d2.Print();
//++d2;
//d2.Print();
Date d1(2001, 9, 18);
d1.Print();
d1--;
d1.Print();
Date d2(2002, 3, 28);
d2.Print();
--d2;
d2.Print();
}
// 测试逻辑判断
void Test04()
{
Date d1(2001, 9, 18);
Date d2(2002, 3, 28);
bool judge = (d1 <= d2);
cout << judge << endl;
}
// 测试日期-日期
void Test05()
{
Date d1(2001, 9, 28);
Date d2(2002, 3, 28);
int day = d1 - d2;
cout << day << endl;
}
// 测试<< 和 >>
void Test06()
{
//Date d1(2001, 9, 28);
//Date d2(2002, 3, 28);
//cout << d1 << d2;
Date d1;
Date d2;
cin >> d1 >> d2;
cout << d1 << d2;
}
int main()
{
//Test01();
//Test02();
//Test03();
//Test04();
//Test05();
Test06();
return 0;
}
(四)const成员函数
使用const修饰成员函数,使用const修饰成员函数之后,类的成员变量就不可修改。
如何使用:在成员函数的后面加上一个const就可以了。
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
// 赋值运算符重载
class Date {
public:
Date(int year = 2001, int month = 1, int day =1)
{
_day = day;
_month = month;
_year = year;
}
~Date()
{}
// 赋值运算符重载:用于两个已经存在的对象之间的拷贝
Date& operator=(const Date& d)// 返回引用能够减少拷贝
{
// 检查自己拷贝自身的情况,
if (this != &d)// 检查两者地址是否一致
{
// 如果地址不一致
_day = d._day;
_month = d._month;
_year = d._year;
}
return *this;// 返回当前被拷贝的对象,这样能够支持连续赋值
}
void print()
{
// 加了const之前不可以修改:this++;
// 加了const之后不可以修改:this->_year = 2010 并且 不可以修改this指针(this++);
cout <<this->_year << " /" << this->_month << " /" << this->_day << endl;
}
private:
int _year;
int _month;
int _day;
};
void Test()
{
Date d1(2001,9,18);
Date d2(2002,9,18);
Date d3(2003,9,18);
cout << "未赋值拷贝" << endl;
d1.print();
d2.print();
d3.print();
d1 = d2 = d3;
cout << "赋值拷贝" << endl;
d1.print();
d2.print();
d3.print();
}
int main()
{
Test();
return 0;
}
原本没加const之前成员函数内部的this指针是这样的:
Date* const this
我们看到类的print成员函数和运算符重载函数,没加const之前:
加了const之后:成员函数里面的this指针变成了这样
const Date* const this
(五)取地址运算符重载
取地址运算符重载分两类:普通取地址运算符重载和const取地址运算符重载。这个运算符成员重载函数也是类的默认函数之一,编译器可以生成,也可以自己写,一般不需要自己写,除非一些特殊的情况。
我们在上述日期类中添加两个&运算符重载函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
// 赋值运算符重载
class Date {
public:
Date(int year = 2001, int month = 1, int day =1)
{
_day = day;
_month = month;
_year = year;
}
~Date()
{}
// 赋值运算符重载:用于两个已经存在的对象之间的拷贝
Date& operator=(const Date& d) // 返回引用能够减少拷贝
{
// 检查自己拷贝自身的情况,
if (this != &d) // 检查两者地址是否一致
{
// 如果地址不一致
_day = d._day;
_month = d._month;
_year = d._year;
}
return *this;// 返回当前被拷贝的对象,这样能够支持连续赋值
}
void print()
{
this->_year = 2010;
// 加了const之前不可以修改:this++;
// 加了const之后不可以修改:this->_year = 2010 并且 不可以修改this指针(this++);
cout <<this->_year << " /" << this->_month << " /" << this->_day << endl;
}
Date* operator&()
{
return this;
//return nullptr;
}
const Date* operator&() const // const Date* operator&(const Date* const this)
{
//return nullptr;
return this;
}
private:
int _year;
int _month;
int _day;
};
void Test()
{
//Date d1(2001,9,18);
//Date d2(2002,9,18);
//Date d3(2003,9,18);
//cout << "未赋值拷贝" << endl;
//d1.print();
//d2.print();
//d3.print();
//d1 = d2 = d3;
//cout << "赋值拷贝" << endl;
//d1.print();
//d2.print();
//d3.print();
Date d1(2001, 9, 18);
cout << &d1.operator=(d1) << endl;
}
int main()
{
Test();
return 0;
}
我们执行之后,发现这个地址是正常的类对象的地址,他是系统开辟栈帧的时候随分配的。
我们将&运算符重载函数修改成空指针或者其他地址之后:
const Date* operator&() const // const Date* operator&(const Date* const this)
{
return nullptr;
//return this;
Date* operator&()
{
return (Date*)0x001fff48;
//return this;
}