この例はhttp://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.htmlで見つけました
#include <iostream>
using namespace std;
class ArrayWrapper
{
public:
// default constructor produces a moderately sized array
ArrayWrapper ()
: _p_vals( new int[ 64 ] )
, _size( 64 )
{}
ArrayWrapper (int n)
: _p_vals( new int[ n ] )
, _size( n )
{}
// move constructor
ArrayWrapper (ArrayWrapper&& other)
: _p_vals( other._p_vals )
, _size( other._size )
{
cout<<"move constructor"<<endl;
other._p_vals = NULL;
}
// copy constructor
ArrayWrapper (const ArrayWrapper& other)
: _p_vals( new int[ other._size ] )
, _size( other._size )
{
cout<<"copy constructor"<<endl;
for ( int i = 0; i < _size; ++i )
{
_p_vals[ i ] = other._p_vals[ i ];
}
}
~ArrayWrapper ()
{
delete [] _p_vals;
}
private:
int *_p_vals;
int _size;
};
int main()
{
ArrayWrapper a(20);
ArrayWrapper b(a);
}
そのクラス内の移動コンストラクターがアクションを実行するいくつかの例 (最も有用な状況) を教えてもらえますか?
この種のコンストラクターの目的は理解できましたが、実際のアプリケーションでいつ使用されるかを正確に特定することはできません。