fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int gCnt = 0;
  6.  
  7. struct Ptr {
  8. int num;
  9. Ptr() : num(++gCnt) { cout << "Ptr::Ptr() " << num << endl; }
  10. Ptr(Ptr const& o) : num(o.num*10 + 2) { cout << "Ptr::Ptr( " << o.num << " ) " << num << endl; }
  11. ~Ptr() { cout << "Ptr::~Ptr() " << num << endl; }
  12. };
  13.  
  14. struct Arr {
  15. Arr() { cout << "Arr::Arr()" << endl; }
  16. ~Arr() { cout << "Arr::~Arr()" << endl; }
  17. operator Ptr() const { cout << "Arr::operator Ptr()" << endl; return Ptr(); }
  18. private:
  19. Arr(Arr const&);
  20. };
  21.  
  22. struct S {
  23. static const Arr a;
  24. };
  25. const Arr S::a;
  26.  
  27. Ptr const& foo() { cout << "call" << endl; Ptr const& r = S::a; cout << "return" << endl; return r; }
  28.  
  29. int main()
  30. {
  31. using namespace std;
  32.  
  33. cout << "a" << endl;
  34. {
  35. cout << "b" << endl;
  36. Ptr const& x = foo();
  37. cout << x.num << endl;
  38. cout << x.num << endl;
  39. cout << "c" << endl;
  40. }
  41. cout << "d" << endl;
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 2856KB
stdin
Standard input is empty
stdout
Arr::Arr()
a
b
call
Arr::operator Ptr()
Ptr::Ptr() 1
return
Ptr::~Ptr() 1
1
1
c
d
Arr::~Arr()