目录
一、 概念
二、特征
正确的拷贝构造函数写法:
拷贝函数的另一种写法
三、若未显式定义,编译器会生成默认的拷贝构造函数。
四、编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗?
深拷贝的写法:
五、拷贝构造函数典型调用场景:
六、总结:
在现实生活中,可能存在一个与你一样的自己,我们称其为双胞胎。
那在创建对象时,可否创建一个与已存在对象一某一样的新对象呢? 拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
拷贝构造函数也是特殊的成员函数,其特征如下:
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d) // 正确写法
{
// this->_year = d._year;
_year = d._year;
_month = d._month;
_day = d._day;
}
Date(Date& d)// 错误写法: 它不能用于从常量对象或临时对象进行拷贝构造
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date(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;
}
Date(const Date& d) // 正确写法
{
// this->_year = d._year;
_year = d._year;
_month = d._month;
_day = d._day;
}
这是正确的拷贝构造函数写法。它接受一个对Date类型的常量引用作为参数,这意味着它可以用于从常量对象、非常量对象甚至是临时对象进行拷贝构造。由于它的灵活性,这是最常用的拷贝构造函数定义方式。
Date(Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date(Date d)// 错误写法:编译报错,会引发无穷递归
{
_year = d._year;
_month = d._month;
_day = d._day;
}
这个构造函数是错误的,会引发无穷递归。原因在于,当试图用这个构造函数创建一个Date对象时,它会尝试以值传递的方式接收一个Date对象作为参数。为了构造这个参数对象d,又需要调用拷贝构造函数,这会导致无限递归调用,最终耗尽栈空间并导致程序崩溃。
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
/*Date(Date& d)
{
d._year = _year;
d._month = _month;
d._day = _day;
}*/
// Date d3(d2);
//Date(const Date& d)
//{
// // this->_year = d._year;
// _year = d._year;
// _month = d._month;
// _day = d._day;
//}
Date(Date* d)
{
_year = d->_year;
_month = d->_month;
_day = d->_day;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
// 给缺省值
int _year = 1;
int _month = 1;
int _day = 1;
};
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 3)
{
cout << "Stack(size_t capacity = 3)" << endl;
_array = (DataType*)malloc(sizeof(DataType) * capacity);
if (NULL == _array)
{
perror("malloc申请空间失败!!!");
return;
}
_capacity = capacity;
_size = 0;
}
// Stack st2 = st1;
Stack(const Stack& st)
{
_array = (DataType*)malloc(sizeof(DataType) * st._capacity);
if (NULL == _array)
{
perror("malloc申请空间失败!!!");
return;
}
memcpy(_array, st._array, sizeof(DataType) * st._size);
_size = st._size;
_capacity = st._capacity;
}
void Push(DataType data)
{
// CheckCapacity();
_array[_size] = data;
_size++;
}
bool Empty()
{
return _size == 0;
}
DataType Top()
{
return _array[_size - 1];
}
void Pop()
{
--_size;
}
// 其他方法...
~Stack()
{
cout << "~Stack()" << endl;
if (_array)
{
free(_array);
_array = NULL;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
int _capacity;
int _size;
};
int main()
{
Date d2(2024, 4, 9);
// 下面这两种写法是等价的
Date d3(d2);
Date d4 = d2; // 这也是拷贝构造
d2.Print();
d3.Print();
//func(d2);
/*Date d3(&d2);
d3.Print();*/
return 0;
}
默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
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;
}
在上述代码中,Date 类并没有显式定义拷贝构造函数。当代码中尝试通过已有的 Date 对象 d1 来拷贝构造一个新的 Date 对象 d2 时,由于没有找到用户定义的拷贝构造函数,编译器会自动为 Date 类生成一个默认的拷贝构造函数。
这个默认生成的拷贝构造函数会完成以下任务:
注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的。
尽管编译器生成的默认拷贝构造函数可以完成字节序的值拷贝,但在某些情况下,仍然需要自己显式实现拷贝构造函数。这主要是因为默认拷贝构造函数执行的是浅拷贝,它会拷贝对象的所有成员变量,但如果对象中包含指针或动态分配的资源(如使用 new 或 malloc 分配的内存),浅拷贝可能会导致问题。
当然像日期类这样的类是没必要的。那么下面的类呢?验证一下试试?
// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
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 s2(s1);
注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
/*Date(Date& d)
{
d._year = _year;
d._month = _month;
d._day = _day;
}*/
// Date d3(d2);
//Date(const Date& d)
//{
// // this->_year = d._year;
// _year = d._year;
// _month = d._month;
// _day = d._day;
//}
Date(Date* d)
{
_year = d->_year;
_month = d->_month;
_day = d->_day;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
// 给缺省值
int _year = 1;
int _month = 1;
int _day = 1;
};
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 3)
{
cout << "Stack(size_t capacity = 3)" << endl;
_array = (DataType*)malloc(sizeof(DataType) * capacity);
if (NULL == _array)
{
perror("malloc申请空间失败!!!");
return;
}
_capacity = capacity;
_size = 0;
}
// Stack st2 = st1;
Stack(const Stack& st)
{
_array = (DataType*)malloc(sizeof(DataType) * st._capacity);
if (NULL == _array)
{
perror("malloc申请空间失败!!!");
return;
}
memcpy(_array, st._array, sizeof(DataType) * st._size);
_size = st._size;
_capacity = st._capacity;
}
void Push(DataType data)
{
// CheckCapacity();
_array[_size] = data;
_size++;
}
bool Empty()
{
return _size == 0;
}
DataType Top()
{
return _array[_size - 1];
}
void Pop()
{
--_size;
}
// 其他方法...
~Stack()
{
cout << "~Stack()" << endl;
if (_array)
{
free(_array);
_array = NULL;
_capacity = 0;
_size = 0;
}
}
private:
DataType* _array;
int _capacity;
int _size;
};
void func(Date& d)
{
d.Print();
}
class MyQueue
{
private:
Stack _st1;
Stack _st2;
int _size = 0;
};
int main()
{
Date d2(2024, 4, 9);
Date d4 = d2;
d4.Print();
Stack st1(10);
st1.Push(1);
st1.Push(1);
st1.Push(1);
Stack st2 = st1;
st2.Push(2);
st2.Push(2);
while (!st2.Empty())
{
cout << st2.Top() << " ";
st2.Pop();
}
cout << endl;
while (!st1.Empty())
{
cout << st1.Top() << " ";
st1.Pop();
}
cout << endl;
MyQueue q1;
MyQueue q2(q1);
return 0;
}
class Date
{
public:
Date(int year, int month, 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;
}
构造函数 Date(int year, int month, int day)
Date(int year, int month, int day)
{
cout << "Date(int,int,int):" << this << endl;
}
拷贝构造函数 Date(const Date& d)
当创建一个已存在Date对象的副本时会调用它。同样,当这个构造函数被调用时,会打印一条消息和当前对象的地址。
Date()
~Date()
{
cout << "~Date():" << this << endl;
}
为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引用。
今天就先到这了!!!