1.概念
拷贝构造函数用于创建一个与当前对象相同的新对象。
拷贝构造函数只有一个参数,形参是对本类类型对象的引用,一般用const修饰。当用一个已存在的对象创建新对象时系统自动调用。
2.特征
1. 拷贝构造函数是构造函数的一个重载形式。
2.1传参导致的无穷递归
1. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,
因为会引发无穷递归调用。
拷贝构造函数写法及使用如下:
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//拷贝构造函数写法
// Date(const Date& d) // 正确写法
Date(const Date d) // 错误写法:编译报错,会引发无穷递归
{
_year = d._year;
_month = d._month;
_day = d._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2(d1); //此处系统自动调用拷贝构造函数
return 0;
}
用d1拷贝d2时,d2调用拷贝构造,d1传值给形参date
d1传值给形参date时,date要调动拷贝构造获得d1的值,此时date调用拷贝构造,d1传值给date′
d1传值给date′时,date′又要调动拷贝构造获得d1的值,此时date′调用拷贝构造,d1传值给date′′。
这样就会一直递归下去。
2.2默认拷贝构造函数及浅拷贝
1. 若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按
字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
#include <iostream>
using namespace std;
class Time
{
public:
Time()
{
_hour = 1;
_minute = 1;
_second = 1;
}
//自定义的浅拷贝
Time(const Time& t)
{
_hour = t._hour;
_minute = t._minute;
_second = t._second;
cout << "Time::Time(const Time&)" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year = 1970;
int _month = 1;
int _day = 1;
// 自定义类型
Time _t;
};
int main()
{
Date d1;
// 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
// 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
Date d2(d1);
return 0;
}
注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定
义类型是调用其拷贝构造函数完成拷贝的。
2.3 深拷贝
1. 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗?
当然像日期类这样的类是没必要的。但若是涉及到动态开辟空间会造成一些问题,如下。
#include <iostream>
using namespace std;
// 这里会发现下面的程序会崩溃掉?这里就需要深拷贝去解决。
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);
return 0;
}
在类中显示定义深拷贝:
Stack(const Stack& s)
{
_array = (DataType*)malloc(s._capacity * sizeof(DataType));
_size = s._size;
_capacity = s._capacity;
for (int i = 0; i < s._capacity; i++)
{
_array[i] = s._array[i];
}
}
给新创建的类的_array也开辟相同大小的空间并赋值可以使新类与旧类的_array地址不同,也就不会造成同一对象两次析构了。
注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请
时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
2.4拷贝构造函数典型调用场景
1. 使用已存在对象创建新对象
2. 函数参数类型为类类型对象
3. 函数返回值类型为类类型对象
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year, int minute, int day)
{
cout << "Date(int,int,int):" << this << endl;
}
Date(const Date& d)
{
cout << "Date(const Date& d):" << this << endl;
}
~Date()
{
cout << "~Date():" << this << endl;
}
private:
int _year;
int _month;
int _day;
};
Date Test(Date d)
{
Date temp(d);
return temp;
}
int main()
{
Date d1(2022, 1, 13);
Test(d1);
return 0;
}
为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用
尽量使用引用。