Advertisement
Guest User

Untitled

a guest
Sep 15th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct A
  4. {
  5.     A()
  6.     {
  7.         std::cout << "A::A()" << std::endl;
  8.     }
  9.     A(A const &other)
  10.     {
  11.         std::cout << "A::A(A const &other)" << std::endl;
  12.     }
  13.     ~A()
  14.     {
  15.         std::cout << "A::~A" << std::endl;
  16.     }
  17. };
  18.  
  19. struct Keeper
  20. {
  21.     A getByValue()
  22.     {
  23.         return a_;
  24.     }
  25.  
  26.     A &getByRef()
  27.     {
  28.         return a_;
  29.     }
  30.  
  31.     A a_;
  32. };
  33.  
  34. int main()
  35. {
  36.     Keeper k;
  37.     {
  38.         A &local = k.getByRef();
  39.         std::cout << "get by ref" << std::endl;
  40.     }
  41.     {
  42.         A const &local = k.getByValue();
  43.         std::cout << "get by value to ref" << std::endl;
  44.     }
  45.  
  46.     //A &local = k.getByValue();
  47.     //error: invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A’
  48.     {
  49.         A local = k.getByValue();
  50.         std::cout << "get by value to value" << std::endl;
  51.     }
  52.  
  53.     std::cout << "program end" << std::endl;
  54.  
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement