Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

class base 
{
public:
    base(const base&) = delete;
    base()
    {
        cout << "construct" << endl;
    }
    ~base() 
    {
        cout << "destruct" << endl;
    }

    int a;
    int b;

    /* The difference explanation I desired is here */
    void operator=(base&& other)  
    // base& operator=(base&& other) // this needs to collaborate with "return *this" 
    {
        this->a = other.a;
        this->b = other.b;
        // return *this;
    }

    /* Not here */
    base& operator=(base& other) = delete;
};

What is the difference between the two versions of operator=(T&&)? They seem both work to me. However, as class member function, the website recommand base& operator=(T&&) version.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
355 views
Welcome To Ask or Share your Answers For Others

1 Answer

In one case, a=b=c works. In the other, it does not.

That is it.

Traditionally, a=b=c does b=c then assigns the result to a. If your operator= returns void, it instead fails to compile.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...