2017-04-02 1 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

関連する問題