目录
赋值运算符重载:用已存在的对象,给另一个已存在的对象赋值
还是使用上节日期类的代码,在测试中如下操作:
- class Date
- {
- public:
- Date(int year = 2000, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- cout << "Date(int,int,int):" << this << endl;
- }
- Date(const Date& d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- cout << "Date(const Date& d):" << this << endl;
- }
- ~Date()
- {
- cout << "~Date():" << this << endl;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- void TestDate()
- {
- Date d1(2022,11,15);//构造函数
- Date d2(d1); //拷贝构造函数
-
- Date d3;//用的默认的参数,创建的时候没给实参
- d3 = d2; //用已存在的对象,给另一个已存在的对象赋值,
-
- Date d4 = d3;//拷贝构造
- }

语法:如果程序员没有显示定义赋值运算符重载,则编译器会自动生成一份
实际情况:编译器不一定会生成,但是编译器一定会完成赋值的操作

这时候会出现浅拷贝,导致内存泄漏问题,那么为了解决这个问题,就需要使用 赋值运算符重载方法,下面依次进行学习
还是从代码来学习:
需求:检测两个日期类型对象是否相等(用的还是上面日期类代码,修改部分如下)
- //需求:检测两个日期类型对象是否相等
- bool IsEqual(const Date& d)
- {
- return _year == d._year &&
- _month == d._month &&
- _day == d._day;
- }
-
- int main()
- {
- Date d1(2022, 11, 15);
- Date d2(d1);
- if (d1.IsEqual(d2))
- {
- cout << "d1 == d2" << endl;
- }
- else
- {
- cout << "d1 != d2" << endl;
- }
-
-
- //上面不直观,要是使用==如下
- if (d1==d2)
- {
- cout << "d1 == d2" << endl;
- }
- else
- {
- cout << "d1 != d2" << endl;
- }
-
- return 0;
- }
修改上述代码
- bool operator==(const Date& d)
- {
- return _year == d._year &&
- _month == d._month &&
- _day == d._day;
- }
-
- if (d1==d2)//与if(d1.operator(d2))等价
- {
- cout << "d1 == d2" << endl;
- }
- else
- {
- cout << "d1 != d2" << endl;
- }

- Date& operator=(const Date& d)
- {
- if(this != &d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- }
- return *this;
- }
原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的 赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数
- Date& operator++()
- {
- _day += 1;
- return *this;
- }
// 后置++:
- Date operator++(int)
- {
- Date temp(*this);
- _day += 1;
- return temp;
- }
- int main()
- {
- Date d;
- Date d1(2022, 1, 13);
- d = d1++; // d: 2022,1,13 d1:2022,1,14
- d = ++d1; // d: 2022,1,15 d1:2022,1,15
- return 0;
- }

A.普通成员函数:没有被const修饰的成员函数
在该成员函数中,可以对对象成员变量进行修改
this类型:Date* const ----》this的指向不能修改,this指向空间的内容可以修改
可写入:可以修改成员变量
- void show()
- {
- _day += 1;
- this->_day += 1; //与上面等价
-
- cout << _year << "/" << _month << "/" << _day << endl;
- }
B const成员函数:被const修饰的成员函数
特性:不能修改“成员变量”
const修饰成员函数实际实在修饰this指针
this的类型:const Date* const
只读:只能读取this中成员,不能修改
注意:如果成员函数内部不一定会修改成员变量,最后将该成员设置const函数
- class Date
- {
- public:
- Date(int year = 2000, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
-
- void show()
- {
- _day += 1;
- this->_day += 1; //与上面等价
-
- cout << _year << "/" << _month << "/" << _day << endl;
- }
- //const成员函数:被const修饰的成员函数
- //特性:不能修改“成员变量”
- //const修饰成员函数实际实在修饰this指针
- //this的类型:const Date* const
- void Print()const
- {
- cout << _year << "/" << _month << "/" << _day << endl;
- }
-
- private:
- int _year;
- int _month;
- int _day;
- };
-
-
- int main()
- {
- //普通对象d1可读可写,既可以调用普通成员函数也可以调用const成员函数
- Date d1(20022, 12, 2);
- d1.show();
- d1.Print();
-
- //const对象d2只读,只能调用const成员函数,只读不能写
- const Date d2(d1);
- d2.Print();
- return 0;
- }
可以
- class Date
- {
- public :
- Date* operator&()
- {
- return this ;
- }
-
- //this的类型就是const Date* const
- const Date* operator&()const
- {
- return this ;
- }
- private :
- int _year ; // 年
- int _month ; // 月
- int _day ; // 日
- };
- int main()
- {
-
- Date d1(20022, 12, 2);
-
- //需求:在对对象取地址的时候需要将地址打印出来
- Date* p = &d1;
-
-
- return 0;
- }
