class MForwardInfo { public: MForwardInfo(MProjectInfo* info) :projectInfo(info) { ; } MForwardInfo() {}; ~MForwardInfo();QString name; QString describe; QStringList dataInfo[5]; MProjectInfo* projectInfo;} 给这个类进行深拷贝构造
时间: 2024-01-21 14:18:41 浏览: 59
Java实现实体类拷贝[深,很深的那种...]
可以这样实现深拷贝构造函数:
```c++
class MForwardInfo {
public:
MForwardInfo(MProjectInfo* info) :projectInfo(new MProjectInfo(*info)) {
name = info->name;
describe = info->describe;
for(int i=0; i<5; i++) {
dataInfo[i] = info->dataInfo[i];
}
}
MForwardInfo() : projectInfo(nullptr) {};
// 深拷贝构造函数
MForwardInfo(const MForwardInfo& other)
: name(other.name), describe(other.describe), projectInfo(new MProjectInfo(*other.projectInfo)) {
for(int i=0; i<5; i++) {
dataInfo[i] = other.dataInfo[i];
}
}
~MForwardInfo() {
delete projectInfo;
}
QString name;
QString describe;
QStringList dataInfo[5];
MProjectInfo* projectInfo;
};
```
需要注意的是,在深拷贝构造函数中,需要手动深拷贝 `projectInfo` 指针所指向的对象,并在析构函数中释放该内存。同时,其他成员变量也需要逐一进行复制,以保证深拷贝的正确性。
阅读全文