簡體   English   中英

為什么移動構造函數不調用?

[英]why move Constructor is not call?

請查看以下示例代碼:

    class testo
{
public:
    testo()
    {
        cout << " default " << endl;
    }

    testo(const testo & src)
    {
        cout << "copy " << endl;
    }
    testo(const testo && src)
    {
        cout << "move" << endl;
    }
    testo & operator=(const testo & rhs)
    {
        cout << " assigment" << endl;
        return *this;
    }
    testo & operator= (const testo && rhs)
    {
        cout << "move" << endl;
    }

};

這是我的功能和主要代碼:

testo nothing(testo & input)
{
return input;
}

int main ()
{
testo boj1 ;
testo obj2(nothing(obj1) );
return 1;
}

當我編譯並運行此代碼時,我希望看到:

default    // default constructor

copy       // returning from the function

move       // moving to the obj2

但是當代碼開始執行時,只顯示:

default 

copy

編譯器是Visual C ++ 2015

應該將移動簽名定義為T&& ,而不是T const && 盡管語言中沒有什么可以阻止您聲明T const && ,但實際上沒有任何意義:您的意思是從該對象移開,但是它是const ,因此不能更改其狀態嗎? 這是術語的矛盾。

暫無
暫無

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

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