malcin

Untitled

Mar 25th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. /*
  2.     Why returning temporary object
  3.     by reference, like:
  4.     Object & ref()
  5.     {
  6.         Object a(); return a;
  7.     }
  8.     isn't good idea.
  9. */
  10.  
  11. #include <iostream>
  12. class simply
  13. {
  14.     int * a;
  15. public:
  16.     int getA()
  17.     {
  18.         return *a;
  19.     }
  20.    
  21.     simply(const simply & copy)
  22.     {
  23.         delete a;
  24.         a = new int;
  25.         *a = *copy.a;
  26.     }
  27.    
  28.     simply(int value)
  29.     {
  30.         a = new int;
  31.         *a = value;
  32.     }
  33.    
  34.     ~simply()
  35.     {
  36.         delete a;
  37.     }
  38. };
  39.  
  40. simply & a()
  41. {
  42.     simply test(5);
  43.     return test;
  44. }
  45.        
  46. int main()
  47. {
  48.     simply c(a());
  49.     std::cout << c.getA();
  50.     // Do we really will get 5? :)
  51. }
Advertisement
Add Comment
Please, Sign In to add comment