domingo, 22 de febrero de 2015

Mover Constructor Copia Visual C++



#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class Person
{
public:

 Person( const char *pN )
 {
  pName = new string( pN );
  cout << "Construyendo " << pName << endl;
 }

 Person( Person &p )
 {
  cout << "Copiando " << pName << endl;
  pName = new string("Copia de " );
  *pName += *p.pName;
 }

 Person( Person &&p )
 {
  cout << "Moviendo " << *p.pName << endl;
  pName = p.pName;
  p.pName = nullptr;
 }

 ~Person()
 {
  if( pName )
  {
   cout << "Destruyendo " << *pName << endl;
   delete pName;
  }

  else
  {
   cout << "Destruyendo objeto vacio" << endl;
  }
 }

protected:
 string *pName;
};

Person fn2( Person p )
{
 cout << "Entrando a fn2" << endl;
 return p;
}

Person fn1( char *pName )
{
 cout << "Entrando a fn1" << endl;
 return fn2( *new Person( pName ) );
}

int main( int argc , char *argv[] )
{
 Person s( fn1("Kenny") );

 system("pause");
 return 0;
}



No hay comentarios:

Publicar un comentario