C++ 如何确保复制构造函数不修改原始对象的指针数据成员?
C++ How do I ensure a copy constructor does not modify an original object's pointer data member?
我用以下 ctor 和 cpy ctor
创建了一个日期和时间 class
Date_and_Times.h
class Date_and_Times {
public:
Date_and_Times(Date, Time *, size_t);
Date_and_Times(const Date_and_Times&);
~Date_and_Times(void);
Date_and_Times& operator=(const Date_and_Times&);
Date_and_Times.cpp
#include "Date_and_times.h"
Date_and_Times::Date_and_Times(Date date, Time *times, size_t N)
:_date(date), _timePtr(times), _size(N){}
Date_and_Times::Date_and_Times(const Date_and_Times& dtObjCpy)
:_date(dtObjCpy._date), _timePtr(dtObjCpy._timePtr), _size(dtObjCpy._size){}
Date_and_Times::~Date_and_Times(void) {}
Date_and_Times& Date_and_Times::operator=(const Date_and_Times &dtObj) {
// check for self-assignment by comparing the address of
// the implicit object and the parameter
if(this == &dtObj)
return *this;
// copy
_date = dtObj._date;
_timePtr = dtObj._timePtr;
_size = dtObj._size;
// return the existing object
return *this;
}
调用 Cpy ctor 以创建新的日期和时间对象:
Date_and_Times dTsObj2(dTsObj);
然后修改新对象:
dTsObj2.addFiveSecs();
但是原始对象也被修改了。
执行顺序:
dTsObj.display(); // original date and time data
Date_and_Times dTsObj2(dTsObj); // copy ctor
dTsObj2.addFiveSecs(); // only the copy should be modified
dTsObj2.display(); // updated date and time data in copy
dTsObj.display(); // date and time data should be the original
输出样本:
输出刚刚创建的对象。
日期是:
1/13/2016
时间是:
10:30:00 10:30:10 10:30:20 10:30:30 10:30:40
输出刚刚使用copy ctor创建的对象:注意下面的对象每个Time对象都加了5秒。
日期是:
1/13/2016
时间是:
10:30:0510:30:1510:30:25 10:30:35 10:30:45
再次输出原始对象以表明它没有被复制者修改。
日期是:
1/13/2016
时间是:
10:30:05 10:30:15 10:30:25 10:30:35 10:30:45
有没有办法让原始对象不被修改?感谢您的洞察力。
问题是您正在复制 _timePtr
指针 ,因此两个对象最终都指向 Time
的同一个实例。结果,当你修改一个时,它们都会改变。
您需要做的是创建 Time
class 的新实例,从 dtObjCpy._timePtr
.
复制它的内容
我用以下 ctor 和 cpy ctor
创建了一个日期和时间 classDate_and_Times.h
class Date_and_Times {
public:
Date_and_Times(Date, Time *, size_t);
Date_and_Times(const Date_and_Times&);
~Date_and_Times(void);
Date_and_Times& operator=(const Date_and_Times&);
Date_and_Times.cpp
#include "Date_and_times.h"
Date_and_Times::Date_and_Times(Date date, Time *times, size_t N)
:_date(date), _timePtr(times), _size(N){}
Date_and_Times::Date_and_Times(const Date_and_Times& dtObjCpy)
:_date(dtObjCpy._date), _timePtr(dtObjCpy._timePtr), _size(dtObjCpy._size){}
Date_and_Times::~Date_and_Times(void) {}
Date_and_Times& Date_and_Times::operator=(const Date_and_Times &dtObj) {
// check for self-assignment by comparing the address of
// the implicit object and the parameter
if(this == &dtObj)
return *this;
// copy
_date = dtObj._date;
_timePtr = dtObj._timePtr;
_size = dtObj._size;
// return the existing object
return *this;
}
调用 Cpy ctor 以创建新的日期和时间对象:
Date_and_Times dTsObj2(dTsObj);
然后修改新对象:
dTsObj2.addFiveSecs();
但是原始对象也被修改了。 执行顺序:
dTsObj.display(); // original date and time data
Date_and_Times dTsObj2(dTsObj); // copy ctor
dTsObj2.addFiveSecs(); // only the copy should be modified
dTsObj2.display(); // updated date and time data in copy
dTsObj.display(); // date and time data should be the original
输出样本:
输出刚刚创建的对象。
日期是:
1/13/2016
时间是:
10:30:00 10:30:10 10:30:20 10:30:30 10:30:40
输出刚刚使用copy ctor创建的对象:注意下面的对象每个Time对象都加了5秒。
日期是:
1/13/2016
时间是:
10:30:0510:30:1510:30:25 10:30:35 10:30:45
再次输出原始对象以表明它没有被复制者修改。
日期是:
1/13/2016
时间是:
10:30:05 10:30:15 10:30:25 10:30:35 10:30:45
有没有办法让原始对象不被修改?感谢您的洞察力。
问题是您正在复制 _timePtr
指针 ,因此两个对象最终都指向 Time
的同一个实例。结果,当你修改一个时,它们都会改变。
您需要做的是创建 Time
class 的新实例,从 dtObjCpy._timePtr
.