简体   繁体   English

赋值运算符重载期间类的指针成员

[英]Pointer members of a class during assignment operator overloading

I'm trying to write a tree construction program in c++. 我正在尝试用c ++编写树构建程序。 (it's McCreight's suffix tree) but i have problem with assignment operator overloading for Nodes, specifically of a pointer attribute within my class Node! (这是McCreight的后缀树),但是我对Node的赋值运算符重载有问题,特别是我类Node中的指针属性重载! I have this code in my tree construction method which is not working (explained below): 我的树构造方法中没有以下代码(下面解释):

void ST::insert(int suffix, Node& leaf)
{
    .
    .
    .
    leaf=find_path(u.suffix);
    cout<<leaf.id<<" "<<leaf.parent->id<<'\n';
    .
    .
    .
}

Node ST::find_path(Node* node, int suffix)
{
    .
    .
    .
    cout<<leaf.parent->id<<'\n';
    return leaf;
}

The cout in find_path prints the correct parent id, but when returning the node to insert() it's parent is being lost. find_path中的cout打印正确的父代ID,但是将节点返回insert()时,其父代丢失了。 cout in insert prints the correct "leaf id" but it doesn't know the "leaf's parent id". 插入中的cout打印正确的“叶子ID”,但不知道“叶子的父ID”。

My code for Node class is like this: 我的Node类代码如下:

class Node
{
public:
    int id;
    Node* parent;
    vector <Node> children;
    vector <int> startPointer;
    Node* SL;
    int strDepth;
    Node()
    {
        parent=NULL;
        SL=NULL;
    }

Node& operator=(const Node node2)
{
    this->id=node2.id;
    if(this != &node2 && node2.parent!=NULL && node2.SL!=NULL)
    {
        *parent = *(node2.parent);
        parent = (node2.parent);
        *SL=*(node2.SL);
    }
    this->children=node2.children;
    this->startPointer=node2.startPointer;
    this->strDepth=node2.strDepth;
}

I have tried many ways to change this overloaded operator but each way giving some other error (usually runtime like NullPointerException), the code I included here is the one which gives the best answer so far, but unless i find a way to know the parent of the returned node i can't finish this! 我已经尝试了多种方法来更改此重载运算符,但是每种方法都会产生一些其他错误(通常是类似NullPointerException这样的运行时),到目前为止,我在此处包含的代码是迄今为止给出最佳答案的代码,但是除非我找到一种了解父级的方法返回的节点中,我无法完成此操作! of course i can return the parent and grandparent as separate nodes but that's not interesting. 当然,我可以将父母和祖父母作为单独的节点返回,但这并不有趣。 Any help is really appreciated. 任何帮助都非常感谢。 Thank you! 谢谢!

Use std::shared_pointer for links to nodes, and std::weak_pointer for backlinks. 使用std::shared_pointer链接到节点,使用std::weak_pointer链接。

You might specialise shared_pointer for your class, so that the added bookkeeping data is stored in the node itself. 您可以为您的类专门设置shared_pointer,以便将添加的簿记数据存储在节点本身中。 Look at enable_shared_from_this<T> . 查看enable_shared_from_this<T>

Your assignment operator is not implemented correctly. 您的分配运算符未正确实施。 Try this instead: 尝试以下方法:

Node& operator=(const Node &node2)
{
    if(this != &node2)
    {
        this->id=node2.id;
        this->parent = node2.parent;
        SL=node2.SL;
        this->children=node2.children;
        this->startPointer=node2.startPointer;
        this->strDepth=node2.strDepth;
    }
    return *this;
}

In which case, you could just omit the operator altogether and let the compiler auto-generate a default operator for you, as it would generate the same code. 在这种情况下,您可以完全省略运算符,并让编译器为您自动生成默认运算符,因为它将生成相同的代码。

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

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