目录
一、const成员函数
1.概念
将const修饰的“成员函数”称之为const成员函数。const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
我们看下面一个例子:
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Display()
{
cout << _year << "-" << _month;
cout<<"-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
const Date d1(2022, 10, 22);
d1.Display();//E1086 对象含有与成员 函数 "Date::Display" 不兼容的类型限定符
return 0;
}
这段代码报错的原因是 我们定义了 d1为const修饰的对象,则&d1的类型为const Date*指针,而这个指针只可以进行读取操作,不可以进行写入操作。而Display函数隐藏了this指针的类型为 Date* const this(或者可以理解为Date* this),但是这个指针是可以进行读写操作的。将&d1赋值给Display函数形参this会使指针的读写权限被放大,所以编译报错。
想要解决这个问题就只要将Display函数也用const修饰即可 。
void Display() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
bool operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return false;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
const Date d1(2022, 10, 22);
Date d2(2023, 4, 25);
d1 > d2;//E0349 没有与这些操作数匹配的 ">" 运算符
d2 > d1;//编译通过
return 0;
}
为什么会出现编译报错呢,原因就是>左边的d1被const修饰,&d1传给重载运算符隐藏的指针this时会出现权限放大问题。 解决问题将>重载也用const修饰即可。
2.成员函数定义的原则:
1.能定义成const的成员函数都应该定义成const,这样const对象(权限平移)和非const对象(权限缩小)都可以调用。
2.要修改成员变量的成员函数,不能定义成const,const对象不能调用,非const才能调用。
二、取地址重载
在类中6个默认的成员函数中还有两个时普通对象和const对象取地址,这两个很少会自己实现。
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
Date* operator&()//用于非const对象取地址
{
return this;
}
const Date* operator&() const//用于const对象取地址
{
return this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
constDate d1(2022, 10, 22);
Date d2(2023, 4, 25);
cout << &d1 << endl;
cout << &d2 << endl;
return 0;
}
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。我们平时使用编译器默认生成的即可。
三、日期类实现
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)const;
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
void print()const;
// 拷贝构造函数
// 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)const;
// 日期-天数
Date operator-(int day)const;
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
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);
private:
int _year;
int _month;
int _day;
};
#include"Date.h"
int Date::GetMonthDay(int year, int month)const
{
assert(year >= 1 && month >= 1 && month <= 12);
int monthArray[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 monthArray[month];
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (_year < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month))
{
print();
cout << "日期非法" << endl;
}
}
void Date::print()const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
// TODO: 在此处插入 return 语句
}
Date::~Date()
{
cout << "~Date" << endl;
}
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 tem(*this);
tem += day;
return tem;
}
Date Date::operator-(int day)const
{
Date tem(*this);
tem -= day;
return tem;
}
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::operator>(const Date& d)const
{
if (_year > d._year)
{
return true;
}
else if(_year==d._year&&_month>d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return false;
}
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) || (*this == d);
}
bool Date::operator<(const Date& d)const
{
return (*this >= d);
}
bool Date::operator<=(const Date& d)const
{
return !(*this > d);
}
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}
int Date::operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date tem(*this);
*this += 1;
return tem;
}
Date Date::operator--(int)
{
Date tem(*this);
*this -= 1;
return *this;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}