目录
此篇文章讲解六个默认成员函数中的 拷贝构造和赋值重载 。
1. 拷贝构造
1.1 概念
拷贝构造:
- 在创建对象的时候用已经创建好的对象去初始化一个新对象;
- 只有一个形参,在用已存在的类类型对象创建新对象时由编译器自动调用。
1.2 特性
- 拷贝构造是构造函数的一个重载形式;
- 拷贝构造的参数只能有一个,且一般为类对象的常引用形式(常引用是因为不想传过来的对象被修改),不能传值,这样会引起无穷递归,编译器会直接报错;
class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } Date(const Date& d) { _day = d._day; _month = d._month; _year = d._year; } void Print() { cout << "今天是" << _year << "年" << _month << "月" << _day << "日" << endl; } private: int _year = 10; int _month = 10; int _day = 10; };
- 当对象采用传值调用的时候,不会把对象直接传过去,编译器会帮你调用类里面的拷贝构造;
- 如果拷贝构造中的参数也是值而不是引用,则会发生无穷递归:
- 若类中未显式定义拷贝构造,则编译器会自动生成一个拷贝构造,这个构造是值拷贝(浅拷贝),内置类型会帮你全部拷贝(按字节),自定义类型会帮你调用它的拷贝构造。
class Time { public: Time(const Time& t) { _hour = t._hour; _min = t._min; _sec = t._sec; cout << "this is time structor!" << endl; } Time(int hour = 1, int min = 1, int sec = 1) { _hour = hour; _min = min; _sec = sec; } private: int _hour; int _min; int _sec; }; class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } private: int _year = 10; int _month = 10; int _day = 10; Time _t; }; int main() { Date d1; Date d2(d1); }
- 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗?当然像日期类这样的类是没必要的,如果像 Stack:
class Stack { public: Stack(int capacity) { int* tmp = (int*)malloc(sizeof(int) * capacity); if (tmp == nullptr) { perror("malloc error"); exit(-1); } _arr = tmp; _size = 0; _capacity = capacity; } void Push(int x) { //CheckCapacity() _arr[_size++] = x; } ~Stack() { if (_arr != nullptr) { free(_arr); _arr = nullptr; } _size = _capacity = 0; } private: int* _arr; int _size; int _capacity; }; int main() { Stack st1(10); Stack st2(st1); }
- st2 拷贝构造st1 ,这里是编译器默认生成的拷贝构造,默认是值拷贝,那么 不管是 int 类型的 size,capacity,还是指针类型的 arr 都会拷贝 st1 的,这样就会导致 st2 中的 _arr 和 st1 中的 _arr 指向同一块空间 ,在析构的时候 st2 会先 free() 那块 malloc 的空间,再析构 st1 的时候,free() 一块已释放的空间,那就会报错。
所以应该添加一份拷贝构造(深拷贝):
class Stack { public: Stack(int capacity) { int* tmp = (int*)malloc(sizeof(int) * capacity); if (tmp == nullptr) { perror("malloc error"); exit(-1); } _arr = tmp; _size = 0; _capacity = capacity; } Stack(const Stack& st) { _size = st._size; _capacity = st._capacity; int* tmp = (int*)malloc(sizeof(int) * _capacity); if (tmp == nullptr) { perror("malloc error"); exit(-1); } _arr = tmp; } void Push(int x) { //CheckCapacity() _arr[_size++] = x; } ~Stack() { if (_arr != nullptr) { free(_arr); _arr = nullptr; } _size = _capacity = 0; } private: int* _arr; int _size; int _capacity; }; int main() { Stack st1(10); Stack st2(st1); }
拷贝构造函数典型调用场景:
- 使用已存在对象创建新对象
- 函数参数类型为类类型对象
- 函数返回值类型为类类型对象
- 为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用。
2. 赋值重载 和 运算符重载
2.1 运算符重载
运算符重载:
- C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
- 函数名字为:关键字operator后面接需要重载的运算符符号。
- 函数原型:返回值类型 operator操作符(参数列表)
注意:
- 不能通过连接其他符号来创建新的操作符:比如operator@
- 重载操作符必须有一个类类型参数
- 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
- 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
- .* :: sizeof ?: . 注意以上5个运算符不能重载。
例如:
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_day = d._day;
_month = d._month;
_year = d._year;
}
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 = 10;
int _month = 10;
int _day = 10;
};
int main()
{
Date d1(2024, 3, 17);
Date d2(d1);
bool ret1 = (d1 == d2);
bool ret2 = (d1.operator==(d2));
cout << ret1 << " " << ret2 << endl;
}
2.2 赋值重载
1. 赋值运算符重载格式
- 参数类型:const T&,传递引用可以提高传参效率
- 返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
- 检测是否自己给自己赋值
- 返回 *this :要复合连续赋值的含义。
class Date { public: Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; } Date(const Date& d) { _day = d._day; _month = d._month; _year = d._year; } bool operator==(const Date& d) { return _year == d._year && _month == d._month && _day == d._day; } Date& operator=(const Date& d) { if (this != &d) { _day = d._day; _year = d._year; _month = d._month; } return *this; } void Print() { cout << "今天是" << _year << "年" << _month << "月" << _day << "日" << endl; } private: int _year = 10; int _month = 10; int _day = 10; }; //int main() //{ // Date d1(2024, 3, 17); // Date d2(d1); // bool ret1 = (d1 == d2); // bool ret2 = (d1.operator==(d2)); // cout << ret1 << " " << ret2 << endl; //} int main() { Date d1(2024, 3, 17); Date d2; d2.operator=(d1); d2.Print(); d1.Print(); bool ret1 = (d1 == d2); bool ret2 = (d1.operator==(d2)); cout << ret1 << " " << ret2 << endl; }
- 赋值重载不能作为全局函数,只能重载成类的成员函数
// 错误示范 // 赋值运算符重载成全局函数,注意重载成全局函数时没有this指针了,需要给两个参数 Date& operator=(Date& left, const Date& right) { if (&left != &right) { left._year = right._year; left._month = right._month; left._day = right._day; } return left; }
原因:如果不在类中定义赋值重载,那么编译器会自动生成一份,这是我们如果再类外声明 了一份,就会和类中默认的产生冲突,所以赋值重载只能是类的成员函数。
- 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类赋值运算符重载完成赋值。
- 如果编译器能够自动生成那么还需要我们去编写赋值重载吗?这里其实和拷贝构造类似,如果不是动态开辟的空间,那么无所谓,但是如果成员变量有需要动态开辟的空间,那么就需要自己编写赋值重载,例如栈类: