gpt4 book ai didi

c++ - 深度复制具有自引用指针的类

转载 作者:行者123 更新时间:2023-11-28 01:28:05 25 4
gpt4 key购买 nike

我有一个 Employee 类,它带有指针变量标记和 char 类型的值以及一个自引用指针子项。我们还有另外两个整型变量“numAttributes”和“numChildren”。 “numChildren”指定可以将 child 添加到类(class)的次数。 “numAttributes”是为了将来的目的。我必须分配和释放内存。为此,我试图实现一个复制构造函数和一个析构函数。我面临的问题是,当子级 var 不是 NULL 时,我无法深度复制整个类(class)。我试过使用 memcpy() 以及提到的解决方案 here .但是我无法正确地做到这一点。当一切顺利时,它会在析构函数中失败。到目前为止我尝试过的是:

#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;
}

完成。

关于c++ - 深度复制具有自引用指针的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52943455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com