链表中的重载分配运算符

时间:2019-03-03 04:55:36

标签: c++ linked-list assignment-operator

class Attribute
{
public:
    string data;
    Attribute *next;

    Attribute& operator = (const Attribute *attr) 
    {
        data = "dummy";
        this->next = NULL;
        return *this;
    }
};

int main()
{

    Attribute* atr = new Attribute("abc");
    Attribute* atr2 = new Attribute("123");

    atr2 = atr;

    atr->data = "etr";
    cout << atr2->data << endl; // Output should be "dummy" but it is "etr"

    system("pause");
    return 0;
}

有人可以建议正确的语法还是可以使此代码起作用的内容?

0 个答案:

没有答案