运算符重载
运算符重载的定义:运算符重载是具有特殊名字的函数,它的名字由operator和后面要定义的运算符共同构成,它和其他函数一样,也具有返回值,参数列表,和函数体
- 运算符重载用于类类型的对象时,可用运算重载的形式定义新的含义。
类类型使用运算符时,必须要有对应的运算符重载函数,如果没有对应的运算符重载函数则会报错 - 重载运算符函数的参数个数和运算符的对象数量一样多,比如一元运算符有一个参数,二元运算符有两个参数,第一个参数是左操作数,第二个参数是右操作数
cout << a;
- 如果运算符重载函数是成员函数的话,第一个参数是this指针隐式传过去的,在实参位置和形参位置可以少写一个参数,也就是说运算符重载函数可以少写一个参数
- 运算符重载函数的结合性和优先性与内置类型一样
- 重载成全局的,面临的是成员私有的问题
下面有四种解决办法:
1. //private
将变量变为公有的
2. 在类中写一个得到私有成员变量值的函数
(类内可以随意访问类内变量不管它是不是私有的)
3. 将运算符重载函数变为类内的成员函数
4.友元声明,在类内声明一个是外部一个函数的友元,外部的函数就可以访问类内的私有变量了
//全局的
bool operator==(const Date& d1, const Date& d2)
{
return d1._year == d2._year
&& d1._month == d2._month
&& d1._day == d2._day;
}
- 注意以下五个运算符不能重载
.* :: sizeof ?: .
C++规定普通函数的函数名就是函数指针
.* 这个运算符用的不多,作用是用指针回调成员函数,了解一下就可以
- 重载运算符至少有一个是类类型参数
不可以改变内置类型本身的含义
int operator+(int a,int b)
不可以这样写
- 哪些类运算是有意义的就重载,没意义的就不重载
比如日期+日期是没意义的
日期 + 天数是有意义的,100天以后 - 重载运算符有前置++和后置++的区别
为了取分前置和后置的区别:后置在形参的位置加上int,
前置什么都不加
// d1++;
// d1.operator++(0);
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
// ++d1;
// d1.operator++();
Date& Date::operator++()
{
*this += 1;
return *this;
}
- << 和 >> 重载为成员函数,第一个参数被this指针占用了,所以第一个参数就是对象的指针,第二个参数是cout, d1 << cout,就和我们的习惯相反了,所以我们要将<< 和 >> 重载为全局的函数,第一个参数为i/o流的引用,第二个参数为对象的引用
赋值运算符重载
赋值运算符重载完成的是两个已经存在的对象直接的拷贝赋值
拷贝构造是一个已经存在的对象拷贝初始化给另一个要创建的对象
赋值运算符重载的特点:
- 必须重载为成员函数,传值传参建议是用const类类型的引用,否则会造成拷贝
- 建议使用传引用返回,可以提高效率,传值返回会形成一份新的拷贝
- 没有显示实现时,会调用编译器默认生成的符值运算符重载,对于内置类型会生成浅拷贝,自定义类型会调用拷贝构造
- 有资源指向的用深拷贝, 没有就不显示实现,用编译器默认生成的拷贝,跟拷贝构造是差不多的
日期类的实现
Date.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
//inline
int GetMonthDay(int year, int month)
{
assert(month > 0&&month < 13);
int arr[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
if ((month == 2) && ((month % 4 == 0) && (month % 100 != 0)) || (month % 400 == 0))
{
return 29;
}
return arr[month];
}
//检查日期是否非法
bool GetCheck()
{
if (_month < 1 || _month > 12 ||
_day < 1 || _day > GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}
//打印
void Print();
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if (!GetCheck())
{
cout << "日期非法";
Print();
}
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
~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);
private:
int _year;
int _month;
int _day;
};
Date.c
#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
全缺省的构造函数
//Date::Date(int year = 1900, int month = 1, int day = 1)
//{
//
//}
//域作用限定符写在函数名的前面
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 析构函数
Date::~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
// 日期+=天数
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -(day);
}
_day += day;
while (_day > GetMonthDay(_year,_month))
{
++_month;
_day -= GetMonthDay(_year, _month);
if (_month == 13)
{
_month = 1;
++_year;
}
}
return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
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)
{
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 日期-天数
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
// 前置++ ++d
Date& Date::operator++()
{
*this += 1;
return *this;
}
// 后置++ d++
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
// 后置-- d--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
// 前置-- --d
Date& Date::operator--()
{
*this -= 1;
return *this;
}
// >运算符重载
bool Date::operator>(const Date& d)
{
if (_year == d._year)
{
if (_month < d._month)
{
return false;
}
else if (_month == d._month)
{
return _day > d._day;
}
}
else if(_year < d._year)
{
return false;
}
return true;
}
// ==运算符重载
bool Date::operator==(const Date& d)
{
return _year == d._year &&
_month == d._month &&
_day == d._day;
}
// >=运算符重载
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)
{
int flag = 1;
Date max = *this;
Date min = d;
if (min > max)
{
min = *this;
max = d;
flag = -1;
}
int n = 0;
if (min != max)
{
++min;
++n;
}
return flag * n;
}
const成员函数
const 修饰的成员函数称之为const成员函数,const在参数列表的后面
比如Print函数中隐含的this指针,本身是Date* const this,加上const后变成了const Date* const this
取地址运算符重载
取地址运算符重载分为普通运算符重载和const 运算符重载,正常情况下,编译器自动生成的就够用,但是特殊情况下,我们不想别人取到当前类对象的地址,想胡乱返回一个地址
class Date
{
public :
Date* operator&()
{
//return this;
// return nullptr;
return (Date*)0x2673FE30;
}
const Date* operator&()const
{
//return this;
// return nullptr;
return (Date*)0x2673FF40;
}
private :
int _year ; // 年
int _month ; // ⽉
int _day ; // ⽇
};