#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class MyContainer
{
public:
MyContainer( int nS , const char *pS ) : nSize( nS )
{
pString = new char[ nSize ];
strcpy( pString , pS );
}
~MyContainer()
{
delete pString;
pString = nullptr;
}
//copiando constructor
MyContainer( const MyContainer &s )
{
copyIt( *this , s );
}
MyContainer &operator=( MyContainer &s )
{
delete pString;
copyIt( *this , s );
return *this;
}
//mueve el constructor
MyContainer &operator=( MyContainer &&s )
{
delete pString;
copyIt( *this , s );
return *this;
}
protected:
static void moveIt( MyContainer &tgt , MyContainer &src )
{
cout << "Moviendo " << src.pString << endl;
tgt.nSize = src.nSize;
tgt.pString = src.pString;
src.nSize = 0;
src.pString = nullptr;
}
static void copyIt( MyContainer &tgt , const MyContainer &src )
{
cout << "Copiando " << src.pString << endl;
delete tgt.pString;
tgt.nSize = src.nSize;
tgt.pString = new char[ tgt.nSize ];
strncpy( tgt.pString , src.pString , tgt.nSize );
}
int nSize;
char *pString;
};
MyContainer fn( int n , const char *pString )
{
MyContainer b( n , pString );
return b;
}
int main( int argc , char *argv[] )
{
MyContainer mc( 100 , "Original");
mc = fn( 100 , "Creada en fn()");
system("pause");
return 0;
}
Herramientas Para el Desarrollo de Videojuegos y Lenguajes de Programación en Español
domingo, 22 de febrero de 2015
Mover Operator Visual C++
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario