fork download
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6. class A {
  7. public:
  8. int i;
  9. A(int i_) : i(i_) {
  10. cout << "A(): " << i << endl;
  11. }
  12. A(const A& a_) : i(a_.i) {
  13. cout << "A(const A&): " << i << endl;
  14. }
  15. ~A() {
  16. cout << "~A(): " << i << endl;
  17. }
  18. };
  19.  
  20. class B {
  21. public:
  22. A a;
  23. int b;
  24. B(const A& a_) : a(a_) {
  25. cout << "B(): " << a.i << endl;
  26. }
  27. ~B() {
  28. cout << "~B(): " << a.i << endl;
  29. }
  30. };
  31.  
  32. int main(void) {
  33. for(int c = 0; c < 3; ++c) {
  34. A a(c+1);
  35. B b(a);
  36. cout << b.a.i << endl;
  37. }
  38. return 0;
  39. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
A(): 1
A(const A&): 1
B(): 1
1
~B(): 1
~A(): 1
~A(): 1
A(): 2
A(const A&): 2
B(): 2
2
~B(): 2
~A(): 2
~A(): 2
A(): 3
A(const A&): 3
B(): 3
3
~B(): 3
~A(): 3
~A(): 3