首页 > 解决方案 > How to store an object inside another object without modifying the original using struct?

问题描述

Contextualization:

When the data structure performs queries it performs rotation transformations in the tree. In this way, the original tree that was loaded is modified. So if I make another appointment instantly, the consultation is different. I need to find a way to keep the original data structure in memory so that the query data structure is a copy of it. Remembering that the data structure in question is an object. That is, how do I copy an object without changing the original?

If I try the code below it doesn't work because I'm making an object receive another object of the same class, so the two are modified:

SPSTTree Taux;
SPSTTree T = operand1->SpstManager;
Taux = T;
Filter_spst(_operand2.dbl, op, Taux);

The object belongs to a struct

typedef struct SPSTNode *PositionSPST;
typedef struct SPSTNode *SPSTTree;
struct SPSTNode{
    ElementType Element;
    int64_t offset;
    SPSTTree lchild;
    SPSTTree rchild;

    int qtd_element = 1;

};

So in summary, I want to pass a data structure to a query .. Understand this data structure as a data indexing tree. This tree is summarized to an object of a class. However, when I perform the query, the structure data is modified. So I need to preserve the original object, which is the loaded tree, to perform other queries instantly without having to load the tree again.

Basically: how to store an object inside another object without modifying the original using struct?

标签: c++oopstruct

解决方案


I am not sure I am understanding well your question, but I guess you have an SPSTTree and you want to be able to make a copy of it so you can modify it without altering the original. To do so you need to implement a recursive copy function for the structure. In this function:

1) Copy the properties of the struct which are not pointers, in your case Element, offset and qtd_element.

2) For all the pointers you need to recursively copy the pointed structs. In your case you have two pointers, lchild and rchild, which point to the corresponding subtrees. So what you need to do is to check for each of them whether they are NULL or not. If they are not NULL call your copy function on the pointed tree (the function calls itself, recursively) and associate the resulting (copied) subtree to the copy you are making.

Example of function (take it as a pseudocode-ish example):

struct SPSTNode{
    ElementType Element;
    int64_t offset;
    SPSTTree lchild;
    SPSTTree rchild;

    int qtd_element = 1;

};
SPSTTree copyTree(SPSTTree original) {
    // Allocate here your copy using the same way you do in your code, as it is not specified in the question I will use malloc as an example
     SPSTTree copy = (SPSTTree) malloc(sizeof(SPSTNode));

     // Copy non-pointer values
     copy.Element = original.Element;
     copy.offset = original.offset;
     copy.qtd_element = original.qtd_element;

     // Recursively copy subtrees
     if (original.lchild) {
           copy.lchild = copyTree(original.lchild);
     } else {
            copy.lchild = 0;
     }
     if(original.rchild) {
           copy.rchild = copyTree(original.rchild);
     } else {
           copy.rchild = 0;
     }
}

Since we are using malloc in this pseudocode-ish example (I am from the phone so I did not try it, however it should be pretty accurate) do not forget to free your memory later!


推荐阅读