Hello!各位铁子们,今天我们要学习的是C++类和对象的第二部分,相信你看完之后一定会有所收获,那就让我们再次一起进入类和对象的世界吧!
默认成员函数就是⽤⼾没有显式实现,编译器⾃动⽣成的成员函数称为默认成员函数。⼀个类,我们不写的情况下编译器会默认⽣成以下6个默认成员函数,其次就是C++11以后还会增加两个默认成员函数,移动构造和移动赋值。
① 构造函数:主要完成初始化工作。
② 析构函数:主要完成清理工作。
③拷贝构造函数:利用同类对象去初始化新创建的对象。
④赋值重载函数:将一个对象赋值给另一个对象。
⑤普通取地址运算符重载。
⑥const取地址运算符重载。
构造函数是类中特殊的成员函数,构造函数虽然名称叫构造函数,但是它的主要任务并不是开辟内存空间和用类创建对象,⽽是在对象实例化时初始化对象。
① 函数名与类名相同。
② ⽆返回值,也不需要写void。
③ 对象实例化时系统会⾃动调⽤对应的构造函数。
④ 构造函数可以重载。
⑤ 如果类中没有显式定义构造函数,则C++编译器会⾃动⽣成⼀个⽆参的默认构造函数,⼀旦⽤⼾显式定义编译器将不再⽣成。
⑥ ⽆参构造函数、全缺省构造函数、用户不写构造函数时编译器默认⽣成的构造函数,都叫做默认构造函数。但是这三个函数有且只有⼀个存在,不能同时存在。⽆参构造函数和全缺省构造函数虽然成函数重载,但是调⽤时会存在歧义。注意这里的默认构造函数指的是不传实参就能调用的函数。
⑦ 用户不写构造函数,编译器默认⽣成的构造函数,对内置类型成员变量的初始化没有要求,具体是否初始化看编译器。对于⾃定义类型成员变量,要求调⽤这个成员变量的默认构造函数来初始化。如果这个成员变量,没有默认构造函数,那么就会报错。这时要初始化这个对象可以用初始化列表。
示例:
#include
using namespace std;
//class Date
//{
//public:
// //有且只能存在一个
// /*Date() //无参构造
// {
// _year = 2000;
// _month = 1;
// _day = 1;
// }*/
// Date(int year = 1999, int month = 2, int day = 2) //全缺省构造
// {
// _year = year;
// _month = month;
// _day = day;
// }
// //Date(int year, int month, int day) //有参构造
// //{
// // _year = year;
// // _month = month;
// // _day = day;
// //}
// void Print()
// {
// cout << _year << "年" << _month << "月" << _day << "日" << endl;
// }
//private:
// int _year;
// int _month;
// int _day;
//};
//
//int main()
//{
// /*Date d1;
// d1.Print();*/
// /*Date d2(2000, 4, 3);
// d2.Print();*/
//
// //Date d3(); //不能加()
// Date d3;
// d3.Print();
// return 0;
//}
class Stack
{
public:
Stack(int n = 4) //全缺省构造
{
int* tmp = (int*)malloc(sizeof(int) * n);
if (tmp == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
arr = tmp;
top = 0;
capacity = n;
}
private:
int* arr;
int top;
int capacity;
};
//两个栈实现一个队列
//调用Stack的构造函数进行初始化
class MyQueue
{
Stack pushstack;
Stack popstack;
};
int main()
{
Stack s1;
MyQueue q1;
return 0;
}
析构函数与构造函数功能相反,析构函数并不是完成对对象本⾝的销毁,⽐如局部对象是存在栈帧的,函数结束栈帧销毁,该局部对象就自动释放了,C++规定对象在销毁时会⾃动调⽤析构函数,完成对对象中资源的清理和释放⼯作。
① 析构函数名是在类名前加上字符 ~。
② 和构造类似,⽆参数⽆返回值。
③ ⼀个类只能有⼀个析构函数。若未显式定义,编译器会⾃动⽣成默认的析构函数。
④ 对象⽣命周期结束时,系统会⾃动调⽤析构函数。
⑤ 跟构造函数类似,我们不写编译器⾃动⽣成的析构函数对内置类型成员不做处理,⾃定义类型成员会自动调⽤它的析构函数。
⑥ 注意若用户显⽰写了析构函数,对于⾃定义类型成员也会调⽤它的析构函数,也就是说⾃定义类型成员⽆论什么情况都会⾃动调⽤析构函数。
⑦ 如果类中没有申请资源时,析构函数可以不写,直接使⽤编译器⽣成的默认析构函数就可以;如果默认⽣成的析构就能满足用户需求,也就不需要显⽰写析构;但是有资源申请时,⼀定要显示写析构函数,否则会造成资源和内存泄漏。
⑧ ⼀个局部域的多个对象,C++规定后定义的先析构。
示例:
class Date
{
public:
Date(int year = 1999, int month = 2, int day = 2) //全缺省构造
{
cout << "Date全缺省构造" << endl;
_year = year;
_month = month;
_day = day;
}
//析构函数---对于Date类,可不写
~Date()
{
cout << "Date析构函数" << endl;
}
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
private:
int _year;
int _month;
int _day;
};
class Stack
{
public:
Stack(int n = 4) //全缺省构造
{
cout << "Stack全缺省构造" << endl;
int* tmp = (int*)malloc(sizeof(int) * n);
if (tmp == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
arr = tmp;
top = 0;
capacity = n;
}
//析构函数---必须写,有资源需要释放
~Stack()
{
cout << "Stack析构函数" << endl;
if (arr)
{
free(arr);
arr = nullptr;
}
top = capacity = 0;
}
private:
int* arr;
int top;
int capacity;
};
//析构时分别调用Stack的析构
class MyQueue
{
Stack pushstack;
Stack popstack;
};
int main()
{
Date d1;
d1.Print();
Stack s1;
cout << endl;
MyQueue q1;
return 0;
}
如果⼀个构造函数的第⼀个参数是⾃⾝类类型的引⽤,且任何额外的参数都有缺省值,则此构造函数
也叫做拷⻉构造函数,也就是说拷⻉构造函数是⼀个特殊的构造函数。
① 拷⻉构造函数是构造函数的⼀个重载。
② 拷⻉构造函数的第⼀个参数必须是类类型对象的引⽤,使⽤传值⽅式编译器直接报错,因为传值方式在语法逻辑上会引发⽆穷递归调⽤。 拷⻉构造函数也可以多个参数,但是第⼀个参数必须是类类型对象的引⽤,后⾯的参数必须有缺省值。
③ C++规定⾃定义类型对象进⾏拷⻉⾏为必须调⽤拷⻉构造,所以这⾥⾃定义类型传值传参和传值返回都会调⽤拷⻉构造函数来完成。
④ 若未显式定义拷⻉构造函数,编译器会⽣成⾃动⽣成拷⻉构造函数。⾃动⽣成的拷⻉构造函数对内置类型成员变量会完成值拷⻉,也是浅拷贝(一个字节一个字节的拷贝),对⾃定义类型成员变量会调⽤它的拷⻉构造函数。
⑤ 如果⼀个类显⽰实现了析构函数并释放了资源,那么它就需要显⽰写拷⻉构造函数,否则就不用写。
⑥ 传值返回会产⽣⼀个临时对象调⽤拷⻉构造,传值引⽤返回,返回的是返回对象的别名(引⽤),没有产⽣拷⻉。但是如果返回对象是⼀个当前函数局部域的局部对象,函数结束它就销毁了,那么使⽤引⽤返回就会出现问题,这时的引⽤就相当于⼀个野引⽤,类似⼀个野指针⼀样。传引⽤返回可以减少拷⻉,但是⼀定要确保返回对象,在当前函数结束后还存在,才能⽤引⽤返回。
示例:
//class Date
//{
//public:
// Date(int year = 1999, int month = 2, int day = 2)
// {
// cout << "Date全缺省构造" << endl;
// _year = year;
// _month = month;
// _day = day;
// }
// //拷贝构造函数
// //Date(Date d) //报错
// Date(const Date& d)
// {
// cout << "拷贝构造函数" << endl;
// _year = d._year;
// _month = d._month;
// _day = d._day;
// }
// Date(Date* d)
// {
// cout << "Date(Date* d)" << endl;
// _year = d->_year;
// _month = d->_month;
// _day = d->_day;
// }
// void Print()
// {
// cout << _year << "年" << _month << "月" << _day << "日" << endl;
// }
//private:
// int _year;
// int _month;
// int _day;
//};
//
Date& func1() //有问题
//Date func1()
//{
// Date d(2023, 10, 1);
// return d;
//}
//
void func2(Date d)
//void func2(Date& d) //传引用减少拷贝,提高效率
//{
// d.Print();
//}
//
//int main()
//{
// //Date d1(2021, 5, 21);
// Date d2(d1);
// //Date d2 = d1; //等价
// //d1.Print();
// //d2.Print();
//
// //Date d3(2022, 2, 2);
// Date d4(&d3);
// //Date d4 = &d3; //等价
// //d3.Print();
// //d4.Print();
//
// Date d5 = func1();
// d5.Print();
//
// func2(d5);
// return 0;
//}
class Stack
{
public:
Stack(int n = 4)
{
cout << "Stack全缺省构造" << endl;
int* tmp = (int*)malloc(sizeof(int) * n);
if (tmp == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
_arr = tmp;
_top = 0;
_capacity = n;
}
//拷贝构造函数
Stack(const Stack& s)
{
cout << "Stack拷贝构造函数" << endl;
_arr = (int*)malloc(sizeof(int) * s._capacity);
if (_arr == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
_top = s._top;
_capacity = s._capacity;
}
void Push(int x)
{
if (_top == _capacity)
{
int newCapacity = _capacity == 0 ? 4 : 2 * _capacity;
int* tmp = (int*)realloc(_arr, sizeof(int) * newCapacity);
if (tmp == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
_arr = tmp;
_capacity = newCapacity;
}
_arr[_top++] = x;
}
~Stack()
{
cout << "Stack析构函数" << endl;
if (_arr)
{
free(_arr);
_arr = nullptr;
}
_top = _capacity = 0;
}
private:
int* _arr;
int _top;
int _capacity;
};
class Queue
{
Stack pushstack;
Stack popstack;
};
int main()
{
/*Stack s1;
s1.Push(1);
s1.Push(2);
Stack s2(s1); *///要显示写拷贝构造,否则会报错
//Stack s2 = s1; //等价
Queue q1;
Queue q2 = q1; //调用栈的拷贝构造
return 0;
}
① 当运算符被⽤于类类型的对象时,C++语⾔允许我们通过运算符重载的形式指定运算符新的含义。C++规定类类型对象使⽤运算符时,必须转换成调⽤对应运算符重载,若没有对应的运算符重载,则会编译报错。
② 运算符重载是具有特殊名字的函数,它的名字是由operator和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有返回类型和参数列表以及函数体。
③ 重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。
④ 如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数⽐运算对象少⼀个。
⑤ 运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。
⑥ 不能通过连接语法中没有的符号来创建新的操作符:⽐如operator@。
⑦ (.* :: sizeof ?: .)以上括号中的5个运算符不能重载。
⑧ 重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int operator+(int x, int y)。
⑨ ⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义。
⑩ 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,为了区分这种情况,C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载。
⑪ 重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第⼀个形参位置,第⼀个形参位置是左侧运算对象,调⽤时就变成了 对象< 示例: (1)定义:赋值运算符重载是⼀个默认成员函数,⽤于完成两个已经存在的对象的直接拷⻉赋值,这里和拷⻉构造不同,拷⻉构造是⽤于⼀个对象拷⻉初始化给另⼀个要创建的对象。 (2)特点 ② 赋值运算符重载有返回值,且建议写成当前类类型引⽤,引⽤返回可以提⾼效率,有返回值的⽬的是为了⽀持连续赋值场景。 ③ 没有显式实现赋值运算符重载时,编译器会⾃动⽣成⼀个默认赋值运算符重载,默认赋值运算符重载的⾏为跟默认拷⻉构造函数类似,对内置类型成员变量会完成值拷⻉,即浅拷⻉(⼀个字节⼀个字节的拷⻉),对⾃定义类型成员变量会调⽤它的赋值重载函数。 ④ 如果⼀个类显⽰实现了析构函数并释放了资源,那么它就需要显⽰写赋值运算符重载,否则就不需要。 ① 将const修饰的成员函数称之为const成员函数,const修饰成员函数放到成员函数参数列表的后 地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,⼀般这两个函数编译器⾃动 OK,各位铁子们,欢乐的时光总是过得很快,又到了说再见的时候了,看到这里,大家对类和对象一定有了更深刻的理解,如果大家觉得小博讲得还好,就快快关注博主吧!!!//报错,至少有一个类类型的参数
//int operator+(int x, int y)
//{
// return x + y;
//}
class Date
{
public:
Date(int year = 1999, int month = 2, int day = 2)
{
cout << "Date全缺省构造" << endl;
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
cout << "拷贝构造函数" << endl;
_year = d._year;
_month = d._month;
_day = d._day;
}
//bool operator==(const Date& d1, const Date& d2) //报错,第一个参数默认是传给this指针
bool operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
private:
int _year;
int _month;
int _day;
};
//解决成员私有问题的方法
//1.让成员变成公有
//2.使用get()函数
//3.友元
//4.重载为成员函数
//bool operator==(const Date& d1,const Date& d2)
//{
// return d1._year == d2._year && d1._month == d2._month && d1._day == d2._day;
//}
int main()
{
Date d1(2023, 9, 1);
Date d2(2023, 9, 1);
//cout << (d1 == d2) << endl;
cout << d1.operator==(d2) << endl; //等价
return 0;
}
2.赋值运算符重载
① 赋值运算符重载是⼀个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成const 当前类类型引⽤,否则传值传参会有拷⻉构造。
示例:class Date
{
public:
Date(int year = 1999, int month = 2, int day = 2)
{
cout << "Date全缺省构造" << endl;
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
cout << "拷贝构造函数" << endl;
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& operator=(const Date& d)
{
cout << "Date赋值重载" << endl;
if (this != &d) //防止给自己赋值
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
private:
int _year;
int _month;
int _day;
};
class Stack
{
public:
Stack(int n = 4)
{
cout << "Stack全缺省构造" << endl;
int* tmp = (int*)malloc(sizeof(int) * n);
if (tmp == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
_arr = tmp;
_top = 0;
_capacity = n;
}
Stack(const Stack& s)
{
cout << "Stack拷贝构造函数" << endl;
_arr = (int*)malloc(sizeof(int) * s._capacity);
if (_arr == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
memcpy(_arr, s._arr, sizeof(int) * s._top);
_top = s._top;
_capacity = s._capacity;
}
//赋值重载
Stack& operator=(const Stack& s)
{
cout << "stack赋值重载" << endl;
if (this != &s)
{
_arr = (int*)malloc(sizeof(int) * s._top);
if (_arr == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
memcpy(_arr, s._arr, sizeof(int) * s._top);
_top = s._top;
_capacity = s._capacity;
}
return *this;
}
void Push(int x)
{
if (_top == _capacity)
{
int newCapacity = _capacity == 0 ? 4 : 2 * _capacity;
int* tmp = (int*)realloc(_arr, sizeof(int) * newCapacity);
if (tmp == nullptr)
{
perror("malloc fail!\n");
exit(1);
}
_arr = tmp;
_capacity = newCapacity;
}
_arr[_top++] = x;
}
~Stack()
{
cout << "Stack析构函数" << endl;
if (_arr)
{
free(_arr);
_arr = nullptr;
}
_top = _capacity = 0;
}
private:
int* _arr;
int _top;
int _capacity;
};
int main()
{
//Date d1(2024, 3, 5);
//Date d2(2025, 8, 1);
d1 = d2; //赋值重载,不是拷贝构造
//d1.operator=(d2);
Stack s1;
s1.Push(1);
s1.Push(2);
s1.Push(3);
Stack s2(s1); //拷贝构造
s1 = s2; //要显示写赋值重载
Stack s3;
s3 = s1 = s2; //连续赋值
return 0;
}
3.日期类的实现
//Date.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
int Date::GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int arr[13] = { -1,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 arr[month];
}
}
bool Date::operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
(*this) -= (-day);
return *this;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date tmp(*this);
/*tmp._day += day;
while (tmp._day > GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);
tmp._month++;
if (tmp._month > 12)
{
tmp._year++;
tmp._month = 1;
}
}*/
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
(*this) += (-day);
return (*this);
}
_day -= day;
while (_day <= 0)
{
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp(*this);
/*tmp._day -= day;
while (tmp._day <= 0)
{
tmp._month--;
if (tmp._month < 1)
{
tmp._year--;
tmp._month = 12;
}
tmp._day += GetMonthDay(tmp._year, tmp._month);
}*/
tmp -= day;
return tmp;
}
bool Date::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;
}
else
{
return false;
}
}
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);
}
//++i
Date& Date::operator++()
{
(*this) += 1;
return *this;
}
//i++
Date Date::operator++(int)
{
Date tmp(*this);
(*this) += 1;
return tmp;
}
//--i
Date& Date::operator--()
{
(*this) -= 1;
return *this;
}
//i--
Date Date::operator--(int)
{
Date tmp(*this);
(*this) -= 1;
return tmp;
}
int Date::operator-(const Date& d)
{
Date maxDate = (*this);
Date minDate = d;
int flag = 1;
if ((*this) < d)
{
maxDate = d;
minDate = (*this);
flag = -1;
}
int n = 0;
while (minDate < maxDate)
{
minDate++;
n++;
}
return n * flag;
}
void Date::Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
//类外用友元
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "-" << d._month << "-" << d._day << endl;
return out;
}
//类外用友元
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
bool Date::CheckValid()
{
if (_year <= 0 || _month <= 0 || _month > 12 || _day < 0 || _day > GetMonthDay(_year, _month))
{
cout << "输入日期非法,请重新输入: " << endl;
return true;
}
return false;
}
//test.cpp
#include "Date.h"
int main()
{
/*Date d1(2024, 11, 23);
Date d2(2024, 11, 23);
cout << (d1 == d2) << endl;*/ //==
//d2 = d1 + 100;
//d2 = d1 + 1000;
//d2 = d1 + 10000;
//d2 = d1 + 50000;
//d2 = d1.operator+(50000); //等价
//d1.Print();
//d2.Print();
//Date d3(2024, 11, 21);
Date d4 = d3 += 100000;
//Date d4 = d3.operator+=(100000); //等价
//d3.Print();
//d4.Print();
//Date d5(2025, 4, 20);
Date d6 = d5 - 1000;
//Date d6 = d5.operator-(1000); //等价
//d5.Print();
//d6.Print();
//Date d7(2025, 5, 1);
Date d8 = d7 -= 10000;
//Date d8 = d7.operator-=(10000); //等价
//d7.Print();
//d8.Print();
//Date d1_1(2024, 8, 14);
//Date d1_2(2024, 8, 14);
//cout << (d1_1 > d1_2) << endl;
//cout << (d1_1 >= d1_2) << endl;
//cout << (d1_1 < d1_2) << endl;
//cout << (d1_1 <= d1_2) << endl;
//cout << (d1_1 != d1_2) << endl;
//Date d3_1(2025, 3, 10);
Date d4 = d3++;
//Date d4_1 = d3_1.operator++(0); //等价,i++
//d3_1.Print();
//d4_1.Print();
d4 = ++d3;
//d4_1 = d3_1.operator++(); //等价,++i
//d3_1.Print();
//d4_1.Print();
//Date d3_2(2025, 2, 3);
Date d4_2 = d3_2--;
//Date d4_2 = d3_2.operator--(0); //等价,i--
//d3_2.Print();
//d4_2.Print();
d4_2 = --d3_2;
//d4_2 = d3_2.operator--(); //等价,--i
//d3_2.Print();
//d4_2.Print();
/*Date d2_1(2025, 12, 21);
Date d2_2(2025, 6, 21);
cout << d2_1 - d2_2 << endl;*/
//Date d3_3;
//Date d3_4(2022, 8, 5);
//cin >> d3_3;
cin >> d3_3 >> d3_4;
//while (d3_3.CheckValid())
//{
// cin >> d3_3;
//}
//cout << d3_3 << endl;
cout << d3_3 << d3_4 << endl;
Date d4_1(2025, 9, 3);
//d4_1 += -11111;
//cout << d4_1 << endl;
d4_1 -= -11111;
cout << d4_1 << endl;
return 0;
}
六.取地址运算符重载
1.const成员函数
⾯。
② const实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进⾏修改。例如:const 修饰Date类的Print成员函数,Print隐含的this指针的类型由 Date* const this 变为 const
Date* const this。
示例: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;
Date d4_2(2021, 4, 13);
d4_2.CheckValid(); //权限缩小
const Date d4_3(2024, 1, 3);
d4_3.CheckValid();
2.取地址运算符重载
⽣成的就够我们⽤了,不需要自己去显⽰实现。除⾮⼀些特殊的场景,⽐如我们不想让别⼈取到当
前类对象的地址,就可以⾃⼰实现⼀份,返回⼀个错误的地址。
示例:class tmpDate
{
public:
tmpDate(int year = 2022, int month = 2, int day = 4)
{
_year = year;
_month = month;
_day = day;
}
tmpDate* operator&()
{
return this;
//return nullptr;
}
const tmpDate* operator&() const
{
return this;
//return nullptr;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
tmpDate d1(2022, 3, 4);
tmpDate d2(2001, 7, 1);
cout << &d1 << endl;
cout << &d2 << endl;
return 0;
}