2017-04-02 87 views
0

請看看下面的示例代碼:爲什麼移動構造函數不是調用?

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

+0

請添加您用於測試課程的代碼。 –

+0

從移動構造函數中移除const限定符/移動賦值 – kreuzerkrieg

+0

對不起,我忘記了函數 – mehdi

回答

0

移動簽名都應該被定義爲T&&,不T const &&。雖然語言中沒有任何東西阻止你聲明T const &&,但從實際意義上講沒有任何意義:你的意思是從這個對象移動,但它的const,因此不能改變它的狀態?這是一個矛盾的條款。

+0

謝謝你的回答。刪除const關鍵字..沒有幫助 – mehdi

相關問題