fork download
  1. #include <iostream>
  2.  
  3. struct A
  4. {
  5. A() { std::cout << this << " constructed via A()\n"; }
  6. A(const A&) { std::cout << this << " constructed via A(const A&)\n"; }
  7. ~A() { std::cout << this << " destructed via ~A()\n"; }
  8. };
  9.  
  10. struct B
  11. {
  12. A a;
  13.  
  14. B(const A& a)
  15. : a(a) { std::cout << this << " constructed via B(const A&)\n"; }
  16.  
  17. ~B() { std::cout << this << " destructed via ~B()\n"; }
  18. };
  19.  
  20. int main()
  21. {
  22. A a;
  23. B b(a);
  24. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
0xbfc32ebe constructed via A()
0xbfc32ebf constructed via A(const A&)
0xbfc32ebf constructed via B(const A&)
0xbfc32ebf destructed via ~B()
0xbfc32ebf destructed via ~A()
0xbfc32ebe destructed via ~A()