fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class A
  7. {
  8. public:
  9. A(const A& other)
  10. : name(other.name)
  11. {
  12. cout << "A(" << name << ")::copy-constructor(), this = " << this << endl;
  13. }
  14. A(string name)
  15. : name(name)
  16. {
  17. cout << "A(" << name << ")::constructor(), this = " << this << endl;
  18. }
  19. ~A()
  20. {
  21. cout << "A(" << name << ")::destructor(), this = " << this << endl;
  22. }
  23. private:
  24. string name;
  25. };
  26.  
  27. class C
  28. {
  29. public:
  30. C(string name, A a)
  31. : name(name), a(a)
  32. {
  33. cout << "C(" << name << ")::constructor()" << endl;
  34. }
  35. ~C()
  36. {
  37. cout << "C(" << name << ")::destructor()" << endl;
  38. }
  39. private:
  40. string name;
  41. A a;
  42. };
  43.  
  44. class B
  45. {
  46. public:
  47. B(string name)
  48. : name(name)
  49. {
  50. cout << "B(" << name << ")::constructor()" << endl;
  51. }
  52. ~B()
  53. {
  54. cout << "B(" << name << ")::destructor()" << endl;
  55. }
  56. private:
  57. string name;
  58. A a1{"a1"};
  59. A a2{"a2"};
  60. C c1{"c1", a1};
  61. A a3{"a3"};
  62. };
  63.  
  64. int main()
  65. {
  66. B b("b1");
  67. return 0;
  68. }
Success #stdin #stdout 0s 3420KB
stdin
Standard input is empty
stdout
A(a1)::constructor(), this = 0xbff3512c
A(a2)::constructor(), this = 0xbff35130
A(a1)::copy-constructor(), this = 0xbff350e8
A(a1)::copy-constructor(), this = 0xbff35138
C(c1)::constructor()
A(a1)::destructor(), this = 0xbff350e8
A(a3)::constructor(), this = 0xbff3513c
B(b1)::constructor()
B(b1)::destructor()
A(a3)::destructor(), this = 0xbff3513c
C(c1)::destructor()
A(a1)::destructor(), this = 0xbff35138
A(a2)::destructor(), this = 0xbff35130
A(a1)::destructor(), this = 0xbff3512c