fork download
  1. #include <iostream>
  2.  
  3. class Foo
  4. {
  5. private:
  6. int m_data;
  7.  
  8. public:
  9. Foo() : m_data(42)
  10. {
  11. std::cout << "Foo constructor\n";
  12. }
  13.  
  14. Foo(const Foo& other)
  15. {
  16. std::cout << "Foo copy constructor\n";
  17. m_data = other.m_data;
  18. }
  19.  
  20. ~Foo()
  21. {
  22. std::cout << "Foo destructor\n";
  23. }
  24. };
  25.  
  26. class Bar
  27. {
  28. Foo m_foo;
  29. public:
  30. Bar(Foo foo)
  31. : m_foo(foo)
  32. {
  33. std::cout << "Bar constructor\n";
  34. }
  35.  
  36. ~Bar()
  37. {
  38. std::cout << "Bar destructor\n";
  39. }
  40. };
  41.  
  42. int main()
  43. {
  44. Foo myFoo;
  45. Bar bar(myFoo);
  46.  
  47. std::cout << " :: End of program scope :: \n";
  48. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Foo constructor
Foo copy constructor
Foo copy constructor
Bar constructor
Foo destructor
 :: End of program scope :: 
Bar destructor
Foo destructor
Foo destructor