Muzer

Bob: C++ deep copy example.

Feb 15th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Bob
  4. {
  5.   public:
  6.     int test;
  7.     int *ptest;
  8.     Bob();
  9.     ~Bob();
  10.     Bob(Bob &copy);
  11. };
  12.  
  13. Bob::Bob()
  14. {
  15.   ptest = new int;
  16. }
  17.  
  18. Bob::~Bob()
  19. {
  20.   delete ptest;
  21. }
  22.  
  23. Bob::Bob(Bob &copy)
  24. {
  25.   test = copy.test;
  26.   ptest = new int;
  27.   *ptest = *(copy.ptest);
  28. }
  29.  
  30. void some_function(Bob bob)
  31. {
  32.   *(bob.ptest) = 5;
  33. }
  34.  
  35. int main()
  36. {
  37.   Bob bob;
  38.   *(bob.ptest) = 7;
  39.   some_function(bob);
  40.   std::cout << *(bob.ptest) << std::endl;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment