简体   繁体   English

复制构造函数和析构函数octree C ++

[英]Copy constructor and destructor octree c++

I have created an Octree data structure but it's not perfect yet. 我已经创建了一个Octree数据结构,但是还不够完善。 I'm struggled with the copy constructor and the destructor. 我为复制构造函数和析构函数而苦恼。 Here is my header file: 这是我的头文件:

class Octree
{
public:
static int lastbranch;
static bool utolsoelotti;

struct node
{
    int value;
    node *child[8];
};

Octree();
~Octree();
Octree(const Octree& oct);

void clear(node* node);
node* searchandset(int dec, int value);
node* search(int dec);
node* step(node *node, int k);
node* copy(node *n);
void Print(node *n)const;
void deletebranch(int branch);
node *root;
};

Constructor,destructor,copy contrsuctor 构造函数,析构函数,复制构造函数

Octree::Octree()
{
root = new node;
root->value = 0;

for (int i = 0; i < 8; i++)
    root->child[i] = 0;
}
Octree::~Octree()
{
clear(root);
}

Octree::Octree(const Octree& oct) {
root = copy(oct.root);
}

void Octree::clear(node *node){
    for (int i = 0; i < 8; i++)
        if (node->child[i])
            clear(node->child[i]);

    delete node;
}

Octree::node*Octree::copy(node *n) {
node* n2 = new node;
if (n) {
    for (int i = 0; i < 8; i++) {
        n2->child[i] = copy(n->child[i]);
    }
}
return n2;
}

And here is how I created objects in the main: 这是我在主体中创建对象的方式:

int main() {

Octree tree;
Octree tree2(tree);

tree.searchandset(8, 2);
tree2.Print(tree2.search(8));
return 0;
}

In the searchandset function I'm giving a value for node number 8 at the first tree. searchandset函数中,我给第一棵树的8号节点提供了一个值。 After that I'm calling the copy constructor and print the 8th node of the second tree. 之后,我要调用复制构造函数并打印第二棵树的第8个节点。 The value is the same what I gave for the first tree, but when the desctructor called I always got this exception: 该值与我为第一棵树提供的值相同,但是当调用调用器时,我总是遇到此异常:

Exception thrown: read access violation. 引发异常:读取访问冲突。 node was 0xDDDDDDDD. 节点为0xDDDDDDDD。

As I know it means that I tried to delete the nodes which I have already deleted. 据我所知,这意味着我试图删除已经删除的节点。 The object 'tree2' is a different object from 'tree' with the same values and nodes isn't it? 对象'tree2'与具有相同值和节点的'tree'是不同的对象,不是吗? Then I don't understand that exception above. 那我不明白上面的那个例外。 I'm new in c++ and I know it's basic stuff, so if somebody would direct me into the right direction I would very appreciate it. 我是c ++的新手,我知道这是基本知识,因此,如果有人将我引向正确的方向,我将非常感激。

The problem lies the in copy function. 问题出在copy功能上。 Let's go it through step by step: 让我们逐步进行:

node* n2 = new node;

if (n) {
    for (int i = 0; i < 8; i++)
        n2->child[i] = copy(n->child[i]);
}
return n2;

For an empty Octree oct , constructed with the default constructor, and copied to another Octree : 对于一个空的Octree oct ,使用默认构造函数构造并复制到另一个Octree

  1. A new node is created, n2 创建一个新noden2
  2. n is the root of oct , so the condition if true noctroot ,因此条件为true
  3. child[i] of n2 has a value of a copy of the corresponding child, so call copy again n2 child[i]具有对应子项的copy的值,因此请再次调用copy
  4. A new node n2 is created 创建一个新节点n2
  5. n is nullptr (because the children where all nullptr in oct ), so don't execute condition nnullptr (因为这里所有的孩子nullptroct ),所以不执行条件
  6. Return n2 返回n2
  7. Repeat steps 3 to 6 8 times 重复步骤36 8次
  8. Return the root n2 返回根n2
  9. Assign new pointer ( n2 ) to root of the copied object 将新的指针( n2 )分配给复制对象的root

But wait! 可是等等! Did you notice that in step 6, you are return ing a new pointer, even though the child is supposed to be nullptr ! 您是否注意到在第6步中,即使该孩子被认为是nullptr ,也要return一个新的指针!

That's the problem, because then, in clear , you will loop through each child. 这就是问题所在,因为这样,您clear会遍历每个孩子。 That's still ok, right? 没关系,对吗? But then, you try to access the children, which are uninitialized (they have random value, the condition will evaluate to true ), so you get a Read access violation , because it's not your memory. 但是,然后,您尝试访问未初始化的子项(它们具有随机值,条件的结果将为true ),因此您会遇到Read access violation ,因为这不是您的内存。

So the solution ? 那么解决方案呢? Only allocate memory for n2 if n is not nullptr . 如果n不为nullptr则仅为n2分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM