简体   繁体   English

为什么赋值运算符不能按预期工作?

[英]Why does assignment operator not work as expected?

UPD: I can see now how stupid this question is, it's just my misunderstanding of C++ constructions. UPD:我现在可以看到这个问题是多么愚蠢,这只是我对 C++ 结构的误解。

I got stuck with operator assignment problem - it doesn't work as expected.我遇到了运算符分配问题 - 它没有按预期工作。 Here's the example code:这是示例代码:

#include <iostream>

class TestClass{
private:
    int pop;
public:
    TestClass(){
        std::cout<<"Default Constuctor\n";
    }
    TestClass(int i):pop(i){
        std::cout<<"Param Constuctor\n";
    }

    TestClass &operator=(const TestClass &other){
        std::cout<<"Assignment Operator \n";
        return *this;
    }

    friend std::ostream &operator<<(std::ostream &out, TestClass &x);
};

std::ostream &operator<<(std::ostream &out, TestClass &x){
    out<<" This is the TestClass with pop=" << x.pop <<"\n";
    return out;
}


int main()
{
    TestClass    P0(333);
    TestClass P_foo(555);

    P_foo = P0;

    std::cout << P0;
    std::cout << P_foo;

    return 0;
}

The result of this program is这个程序的结果是

Param Constuctor
Param Constuctor
Assignment Operator 
 This is the TestClass with pop=333
 This is the TestClass with pop=555

So P_foo object preserves the initialized value 555. If I comment out my custom assignment operator, the program works as expected.所以P_foo对象保留了初始化值 555。如果我注释掉我的自定义赋值运算符,程序会按预期工作。

Param Constuctor
Param Constuctor
 This is the TestClass with pop=333
 This is the TestClass with pop=333

What's wrong with my assignment operator function?我的赋值运算符函数有什么问题?

What's wrong with my assignment operator function?我的赋值运算符函数有什么问题?

At no point does *this get modified in your implementation of operator= : *this在您的operator=实现中绝不会被修改:

    TestClass &operator=(const TestClass &other){
        std::cout<<"Assignment Operator \n";
        return *this;
    }

I see several issues with this code:我看到这段代码有几个问题:

  • pop is not being initialized in the default constructor (which you are not calling, but you should still implement it properly, if you are going to implement it manually at all). pop没有在默认构造函数中初始化(您没有调用它,但如果您打算手动实现它,您仍然应该正确实现它)。

  • operator= is not updating the value of this->pop at all, which is the root cause of your issue. operator=根本没有更新this->pop的值,这是您问题的根本原因。 If you declare operator= , you are responsible for implementing it properly yourself, the compiler will not help you at all.如果你声明operator= ,你有责任自己正确实现它,编译器根本不会帮助你。 But if you do not declare operator= , the compiler will auto-generate a default implementation that will assign a copy of the pop value for you.但是如果您不声明operator= ,编译器将自动生成一个默认实现,该实现将为您分配一个pop值的副本。

  • operator<< should be taking the TestClass parameter by const reference. operator<<应该通过const引用来获取TestClass参数。

Try this:尝试这个:

#include <iostream>

class TestClass{
private:
    int pop;
public:
    TestClass() : pop(0) { // <-- add this value!
        std::cout << "Default Constructor\n";
    }

    TestClass(int i) : pop(i) {
        std::cout << "Param Constructor\n";
    }

    TestClass& operator=(const TestClass &other){
        std::cout << "Assignment Operator\n";
        pop = other.pop; // <-- add this!
        return *this;
    }

    friend std::ostream& operator<<(std::ostream &out, const TestClass &x);
};

std::ostream& operator<<(std::ostream &out, const TestClass &x){
    out << " This is the TestClass with pop=" << x.pop << "\n";
    return out;
}

int main()
{
    TestClass    P0(333);
    TestClass P_foo(555);

    P_foo = P0; // <-- this will work as expected now

    std::cout << P0;
    std::cout << P_foo;

    return 0;
}
TestClass &operator=(const TestClass &other){
    std::cout << "Assignment Operator \n";
    pop = other.pop;    // You need to add this
    return *this;
}

The issue is that the assignment operator you defined doesn't assign anything to the instance it is called on.问题是您定义的赋值运算符没有将任何内容分配给它被调用的实例。

Read through your operator again:再次通读您的运营商:

TestClass &operator=(const TestClass &other){
    std::cout<<"Assignment Operator \n";
    return *this;
}

You can see that two things happen.你可以看到发生了两件事。 Assignment Operator is printed, and *this is returned.打印Assignment Operator ,并返回*this But your "other" TestClass is not used at all, and the data in this is never modified.但是,你的“其他” TestClass中完全不使用,在数据this是永远不会改变。

You can fix that by assigning to this->pop like so:您可以通过像这样分配给this->pop来解决this->pop

TestClass &operator=(const TestClass &other){
    std::cout<<"Assignment Operator \n";
    pop = other.pop; // Now this->pop will be assigned a new value from other.pop
    return *this;
}

Now when you assign P_foo = P0 , P_foo is successfully assigned the pop value from P0 .现在,当您分配P_foo = P0P_foo已成功分配来自P0的 pop 值。

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

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