I have base class with copy and move constructors like this:
class Test {
public:
Test( int i ) {
iptr = new int( i );
}
Test( const Test & other ) {
printf("copy constructor\n");
iptr = new int( *other.iptr );
}
Test( Test && other ) {
printf("move constructor\n");
iptr = other.iptr;
other.iptr = NULL;
}
virtual ~Test() {
delete iptr;
}
virtual Test * copy() {
return new Test( *this );
}
virtual Test * move() && {
return new Test( *this );
}
protected:
int * iptr;
};
I added a copy and move methods to allow polymorphic copying and moving the object from a pointer, that can potentialy point to an instance of some subclass.
However when i write the following
Test t1( 5 );
Test * t2 = t1.copy();
Test * t3 = Test( 6 ).move();
first case correctly calls copy constructor, but second case incorrectly calls copy constructor too.
Why does the constructor overloading not work properly and how do i make it to call the move constructor?