深层复制具有自引用指针的类
Deep copy a class having a self reference pointer
我有一个带有指针变量标签和字符类型值的类员工和一个自引用指针子级。我们还有另外两个整数变量"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;
}
并完成。
相关文章:
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 深层复制具有自引用指针的类
- C++ 关于指针取消引用的技术问题
- 在 Rust 中,指针和引用有什么区别?
- <Base> <Derived> 具有相同原始指针共享引用的 shared_ptr 和 shared_ptr 实例是否计数?
- 为什么我不能直接从返回指针的函数返回对指针的引用?
- 通过指针或引用将映射传递给函数?
- 自引用 c++20 概念
- 将指针与引用进行比较
- 使用基于范围的for循环取消对矢量指针的引用
- 什么是常量指针常量引用类型的参数?(const X* const & p)
- 共享指针取消引用错误
- 从函数返回对指针的引用与返回指针相同?
- C++指针和对指针的引用
- ctypes 不允许多次取消对指针的引用
- 使用指针和引用,无法弄清楚为什么DFS无限重复
- 将类型参数传递给自引用指针
- 如何为具有自引用指针的类实现复制构造函数/赋值运算符
- 自引用指针算术
- 为什么在c++中可以将解引用指针自增为常量数据?