我在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);
}
有人可以给我一些例子(最有用的情况),该类中的移动构造函数采取行动吗?
我理解这种构造函数的目的,但我无法确定它何时会在实际应用程序中使用。