前言:
上篇博客中,我们学习了运算符重载函数的定义、格式与特征,介绍了众多运算符的重载编 写过程:C++基础——运算符重载函数
这次我们来学习一个新的运算符重载函——赋值运算符重载函数。
说一下运算符重载函数的由来:
运算符重载函数是用来体现运算符的多态性,在学习C语言的过程中,运算符全都是用来解决内置类型数据、逻辑、关系表达式间的运算,而在学习C++的过程中,我们了解了类与对象,就会思考到自定义类型的对象之间是否也能进行+-*/等多种运算呢?基于此种想法,C++官方研发出了运算符重载函数,就是为了让自定义类型也能做运算,体现出了语言的多态性。
目录
前言:
一.赋值运算符重载
赋值运算符重载函数和拷贝构造有着很相似的地方,都是将一个对象的数据拷贝赋值给另一个对象。但不同点在于:
拷贝构造函数是一个拷贝初始化另一个马上要创建的对象;
而赋值运算符重载是两个对象都已经创建好后,进行拷贝赋值。
Date d0(1500, 3, 17);
//拷贝构造
Date d00(d0);
Date d01 = d0; //也属于拷贝构造,它等价于 Date d01(d0);
d01 = d0; //赋值重载
练习:
//赋值运算符重载
class Date {
public:
Date(int year = 1199, int month = 12, int day = 15) {
_year = year;
_month = month;
_day = day;
}
void operator=(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
}
void Print() {
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main() {
Date d1(2022, 10, 19);
Date d2(2021, 5, 17);
d1.Print();
d1 = d2; //调用operator=赋值运算符重载函数
d1.Print();
return 0;
}
void operator=这种写法只能支持单次赋值操作,而我们之前应该写过:
int a=10,b=20,c=50;
a=b=c;
这种赋值方式称为连续赋值——链式访问,它是通过将c值传给b,然后其返回值再传给a,我们当前的函数没有返回值,所以它只能是一次性的赋值,若想要变成连续赋值函数,需要:
//支持连续赋值
Date& operator=(const Date& d) {
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
解析:使用引用返回的目的是为了减少拷贝次数,提高运行效率,对于引用返回不太明白的小伙伴可以去看看: C++基础——引用讲解2
里面介绍了传值返回和引用返回的区别。
return *this时,this是局部指针,出了作用域会被销毁,但*this不会,*this是对象d1,d1的生命周期是全局性的,所以可以使用引用返回。
二.默认运算符重载函数
当我们不写赋值重载函数时,编译器会自动生成一个默认的赋值重载函数,它的运算方式是按照字节序的方式逐步完成拷贝。
练习:
class Date {
public:
Date(int year = 1199, int month = 12, int day = 15) {
_year = year;
_month = month;
_day = day;
}
//没有写赋值重载函数
void Print() {
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main() {
Date d1(2022, 10, 30);
Date d2(1000, 1, 1);
Date d3(2090, 12, 10);
cout << "d1更改前:" << endl;
d1.Print();
d1 = d2;
cout << "d1更改后:" << endl;
d1.Print();
cout << "d2更改前:" << endl;
d2.Print();
d2 = d1 = d3;
cout << "d2更改后:" << endl;
d2.Print();
return 0;
}
注:内置类型成员变量是以字节序的方式直接赋值,而自定义类型成员变量是需要调用对应类的赋值重载函数完成赋值。
所以日期类的赋值重载并不需要亲自去写。
需要自己写赋值重载的类为:
//需要自己写赋值的有Stack类:
typedef int DataType;
class Stack{
public:
Stack(size_t capacity = 10){
_array = (DataType*)malloc(capacity * sizeof(DataType));
if (nullptr == _array){
perror("malloc申请空间失败");
return;
}
_size = 0;
_capacity = capacity;
}
void Push(const DataType& data){
// CheckCapacity();
_array[_size] = data;
_size++;
}
~Stack(){
if (_array){
free(_array);
_array = nullptr;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
size_t _size;
size_t _capacity;
};
int main(){
Stack s1;
s1.Push(1);
s1.Push(2);
s1.Push(3);
s1.Push(4);
Stack s2;
s1 = s2; //赋值重载
return 0;
}
若我们使用默认的赋值重载函数,会报错,原因与之前拷贝构造中Stack类的错误原因相同,都是对同一块空间析构了两次,不仅如此,还存在内存泄漏。
针对这两个错误,我们需要自己去写赋值重载函数。若想不造成内存泄漏和析构错误,那么我们需要把左值对象的_array所指向的空间给提前释放掉,然后再重新malloc一个与被拷贝对象空间同等大小的空间,这样一来,两个问题都会被解决。
//赋值运算符重载函数
Stack& operator=(const Stack& st) {
free(_array);
_array = (DataType*)malloc(sizeof(DataType) * st._capacity);
if (_array == nullptr) {
perror("malloc fail");
exit(-1);
}
memcpy(_array, st._array, sizeof(DataType) * st._size);
_size = st._size;
_capacity = st._capacity;
}