深层复制具有自引用指针的类

Deep copy a class having a self reference pointer

本文关键字:指针 自引用 复制      更新时间:2023-10-16

我有一个带有指针变量标签和字符类型值的类员工和一个自引用指针子级。我们还有另外两个整数变量"numAttributes"和"numChildren"。"numChildren"指定如何将孩子添加到类中。 "numAttributes"用于将来的目的。我必须分配和释放内存。为此,我正在尝试实现复制构造函数和析构函数。我面临的问题是,当它有孩子 var 而不是 NULL 时,我无法深度复制整个班级。我尝试使用memcpy((以及这里提到的解决方案。但我无法正确地做到这一点。当一切顺利时,它在析构函数中失败。到目前为止,我尝试的是:

#include<iostream>
#include<stdlib.h>
#include<string>
#include<map>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
class Employee
{
public:
char* tag;
char* value;
int numAttributes;
int numChildren;
Employee* children;
Employee(const Employee &attr)
{
cout << "Copy constructor called" << endl;
numAttributes = attr.numAttributes;
numChildren = attr.numChildren;
tag = (char*)malloc(sizeof(char)*strlen(attr.tag) + 1);
value = (char*)malloc(sizeof(char)*strlen(attr.value) + 1);
strcpy(tag, attr.tag);
strcpy(value, attr.value);
if (attr.children == NULL)
children = NULL;
else
children = attr.children; // shallow copy happening. Have to do deep copy if it has children
}
Employee(){
cout << " constructor called" << endl;
tag = NULL;
value = NULL;
children = NULL;
numAttributes = 0;
numChildren = 0;
}
~Employee(){
cout << "Destructor called" << endl;
if (tag != NULL){
free(tag);
tag = NULL;
}
if (value != NULL){
free(value);
value = NULL;
}
if (children != NULL){
free(children);
children = NULL;
}
}
};
Employee createNode(const char* tag, const char* value, unsigned int numAttributes, unsigned int numChildren)
{
Employee retNode;
retNode.tag = (char*)malloc(sizeof(char)* (strlen(tag) + 1));
strcpy(retNode.tag, tag);
retNode.value = (char*)malloc(sizeof(char)* (strlen(value) + 1));
strcpy(retNode.value, value);
retNode.numAttributes = numAttributes;
retNode.numChildren = numChildren;
//use this block if we are not initializing the children in the createOffset() method
/*if (numChildren == 0){
retNode.children = NULL;
}
else{
retNode.children = (Employee*)malloc(sizeof(Employee)*numChildren);
}*/
return retNode;
}
Employee createOffset()
{
//Create and tag initial root node
Employee retNode = createNode("offset", "value", 0, 1);
retNode.children = (Employee*)malloc(sizeof(Employee)*retNode.numChildren);
retNode.children[0] = createNode("prnt", "0000", 0, 0);
return retNode; // Until here it is fine. This return calls the copy constructor first. As it has children the children must also be deep copied. Getting error here. Have to do deep copy the entire the class
}
Employee get(){
return createOffset();
}
int main(){
Employee node = get();
getchar();
return 0;
}
struct Employee {
std::string tag;
std::string value;
int numAttributes = 0;
std::vector<Employee> children;
};

无需编写复制构造函数或析构函数,C++语言为您制作了一个在这里做正确事情的语言。

这称为 0 规则。

Employee createNode(const char* tag, const char* value, unsigned int numAttributes, unsigned int numChildren)
{
return {tag, value, numAttributes, std::vector<Employee>(numChildren)};
}

也短了很多。

Employee createOffset()
{
//Create and tag initial root node
Employee retNode = createNode("offset", "value", 0, 1);
retNode.children[0] = createNode("prnt", "0000", 0, 0);
return retNode;
}

并完成。