簡體   English   中英

為什么賦值運算符不能按預期工作?

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

UPD:我現在可以看到這個問題是多么愚蠢,這只是我對 C++ 結構的誤解。

我遇到了運算符分配問題 - 它沒有按預期工作。 這是示例代碼:

#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;
}

這個程序的結果是

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

所以P_foo對象保留了初始化值 555。如果我注釋掉我的自定義賦值運算符,程序會按預期工作。

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

我的賦值運算符函數有什么問題?

我的賦值運算符函數有什么問題?

*this在您的operator=實現中絕不會被修改:

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

我看到這段代碼有幾個問題:

  • pop沒有在默認構造函數中初始化(您沒有調用它,但如果您打算手動實現它,您仍然應該正確實現它)。

  • operator=根本沒有更新this->pop的值,這是您問題的根本原因。 如果你聲明operator= ,你有責任自己正確實現它,編譯器根本不會幫助你。 但是如果您不聲明operator= ,編譯器將自動生成一個默認實現,該實現將為您分配一個pop值的副本。

  • operator<<應該通過const引用來獲取TestClass參數。

嘗試這個:

#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;
}

問題是您定義的賦值運算符沒有將任何內容分配給它被調用的實例。

再次通讀您的運營商:

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

你可以看到發生了兩件事。 打印Assignment Operator ,並返回*this 但是,你的“其他” TestClass中完全不使用,在數據this是永遠不會改變。

您可以通過像這樣分配給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;
}

現在,當您分配P_foo = P0P_foo已成功分配來自P0的 pop 值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM