fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Foo
  5. {
  6. int x;
  7.  
  8. public:
  9. Foo() : x(0) { cout << "Foo()" << endl; }
  10. Foo(int x) : x(x) { cout << "Foo(int)" << endl; }
  11. Foo& operator=(const Foo& o) {
  12. cout << "Foo::operator=(const Foo&)" << endl;
  13. this->x = o.x; return *this;
  14. }
  15. };
  16.  
  17. class Bar
  18. {
  19. Foo foo;
  20. public:
  21. Bar(const Foo& foo)
  22. {
  23. this->foo = foo;
  24. }
  25.  
  26. Bar(bool, const Foo& foo) : foo(foo)
  27. {
  28.  
  29. }
  30. };
  31.  
  32. int main() {
  33. cout << "Assigned in constructor" << endl;
  34. Bar bar = Bar(Foo(5));
  35. cout << "Assigned in initializer list" << endl;
  36. Bar bar2 = Bar(false, Foo(5));
  37. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Assigned in constructor
Foo(int)
Foo()
Foo::operator=(const Foo&)
Assigned in initializer list
Foo(int)