fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. class A
  5. {
  6. public:
  7. A(std::string& s) : s_(s)
  8. { std::cout << "A::ctor" << std::endl; }
  9.  
  10. A(const A& rhs) : s_(rhs.s_)
  11. { std::cout << "A::copy" << std::endl; }
  12.  
  13. ~A()
  14. { std::cout << "A::dtor" << std::endl; }
  15.  
  16. A& operator=(const A& rhs)
  17. { std::cout << "A::copyassign" << std::endl; }
  18.  
  19. private:
  20. std::string& s_;
  21. };
  22.  
  23. std::string s("hello world");
  24.  
  25. int main()
  26. {
  27. A a(s); // a.s_ references the global s.
  28. A b(a); // b.s_ references the a.s_ that references the global s.
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 2812KB
stdin
Standard input is empty
stdout
A::ctor
A::copy
A::dtor
A::dtor