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
請添加您用於測試課程的代碼。 –
從移動構造函數中移除const限定符/移動賦值 – kreuzerkrieg
對不起,我忘記了函數 – mehdi