#include <iostream>
struct base
{
base() { std::cout << "ctor" << std::endl; }
base( const base& ) { std::cout << "copy ctor" << std::endl; }
base& operator = ( const base& ) { std::cout << "copy op" << std::endl; }
base( base&& right ) noexcept { std::cout << "move ctor" << std::endl; }
base& operator = ( base&& right ) noexcept { std::cout << "move op" << std::endl; }
~base() { std::cout << "dtor" << std::endl; }
};
namespace A
{
struct sth : base {};
}
namespace B
{
struct sth : base
{
sth() {}
sth( const sth& ) {}
sth& operator = ( const sth& ) {}
sth( sth&& ) {}
sth& operator = ( sth&& ) {}
~sth() {}
};
}
namespace C
{
struct sth : base
{
sth() {}
sth( const sth& right ) : base( right ) {}
sth& operator = ( const sth& right )
{
// copy base's members
static_cast < base& >( *this ) = right;
/* copy sth's members */
}
sth( sth&& right ) : base( std::move( right ) ) { /* right is no longer available! */ }
sth& operator = ( sth&& right )
{
// move base's members
static_cast < base& >( *this ) = std::move( right );
/* move sth's members
* but now right is not available any longer! */
}
~sth() {}
};
}
//
// main
//
int main()
{
std::cout << "case A:" << std::endl;
{ // case A
A::sth s1;
A::sth s2 = s1;
A::sth s3 = std::move( s1 );
s1 = s3;
}
std::cout << "case B:" << std::endl;
{ // case B
B::sth s1;
B::sth s2 = s1;
B::sth s3 = std::move( s1 );
s1 = s3;
}
std::cout << "case C:" << std::endl;
{ // case C
C::sth s1;
C::sth s2 = s1;
C::sth s3 = std::move( s1 );
s1 = s3;
}
}