深度复制具有自引用指针的 class
Deep copy a class having a self reference pointer
我有一个 class 员工,其指针变量标记和 char 类型的值以及一个自引用指针 children。我们还有另外两个整型变量 "numAttributes" 和 "numChildren"。 "numChildren" 指定如何将 children 添加到 class。 "numAttributes" 供将来使用。我必须分配和释放内存。为此,我试图实现一个复制构造函数和一个析构函数。我面临的问题是,当 class 的 children 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;
}
完成。
我有一个 class 员工,其指针变量标记和 char 类型的值以及一个自引用指针 children。我们还有另外两个整型变量 "numAttributes" 和 "numChildren"。 "numChildren" 指定如何将 children 添加到 class。 "numAttributes" 供将来使用。我必须分配和释放内存。为此,我试图实现一个复制构造函数和一个析构函数。我面临的问题是,当 class 的 children 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;
}
完成。