I found this example on 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);
}
Could someone give me some examples (the most useful situations) where the move constructor inside that class takes action?
I understood the aim of this kind of constructor, but I cannot identify when exactly it will be used in a real application.