Advertisement
obernardovieira

Copy pointers with operator= (overloading)

Jan 22nd, 2016
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6. class MeuObjeto
  7. {
  8. public:
  9.     MeuObjeto(int xx, int yy);
  10.     ~MeuObjeto();
  11.  
  12.     MeuObjeto& operator=(const MeuObjeto& mo);
  13. private:
  14.     int x;
  15.     int y;
  16. };
  17.  
  18. MeuObjeto::MeuObjeto(int xx, int yy)
  19. {
  20.     x = xx;
  21.     y = yy;
  22.     cout << "Construtor " << x << " " << y << endl;
  23. }
  24.  
  25. MeuObjeto::~MeuObjeto()
  26. {
  27.     cout << "Destrutor " << x << " " << y << endl;
  28. }
  29.  
  30. MeuObjeto& MeuObjeto::operator=(const MeuObjeto& mo) {
  31.     x = mo.x;
  32.     //y
  33.     cout << "Copia" << endl;
  34.     return *this;
  35. }
  36.  
  37. int main() {
  38.  
  39.     MeuObjeto* mo1 = new MeuObjeto(1, 3);
  40.     MeuObjeto* mo2 = new MeuObjeto(2, 4);
  41.    
  42.     *mo2 = *mo1;
  43.    
  44.     delete mo2;
  45.     delete mo1;
  46.    
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement