#include <iostream>
using namespace std;
class Foo
{
int x;
public:
Foo() : x(0) { cout << "Foo()" << endl; }
Foo(int x) : x(x) { cout << "Foo(int)" << endl; }
Foo& operator=(const Foo& o) {
cout << "Foo::operator=(const Foo&)" << endl;
this->x = o.x; return *this;
}
};
class Bar
{
Foo foo;
public:
Bar(const Foo& foo)
{
this->foo = foo;
}
Bar(bool, const Foo& foo) : foo(foo)
{
}
};
int main() {
cout << "Assigned in constructor" << endl;
Bar bar = Bar(Foo(5));
cout << "Assigned in initializer list" << endl;
Bar bar2 = Bar(false, Foo(5));
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgRm9vCnsKICBpbnQgeDsKICAKcHVibGljOgogIEZvbygpIDogeCgwKSB7IGNvdXQgPDwgIkZvbygpIiA8PCBlbmRsOyB9CiAgRm9vKGludCB4KSA6IHgoeCkgeyBjb3V0IDw8ICJGb28oaW50KSIgPDwgZW5kbDsgfQogIEZvbyYgb3BlcmF0b3I9KGNvbnN0IEZvbyYgbykgeyAKICAJY291dCA8PCAiRm9vOjpvcGVyYXRvcj0oY29uc3QgRm9vJikiIDw8IGVuZGw7IAogIAl0aGlzLT54ID0gby54OyByZXR1cm4gKnRoaXM7CiAgfQp9OwoKY2xhc3MgQmFyCnsKICAgRm9vIGZvbzsKcHVibGljOiAgCiAgIEJhcihjb25zdCBGb28mIGZvbykKICAgewogICAgIHRoaXMtPmZvbyA9IGZvbzsKICAgfQogICAKICAgQmFyKGJvb2wsIGNvbnN0IEZvbyYgZm9vKSA6IGZvbyhmb28pCiAgIHsKICAgCQogICB9Cn07CgppbnQgbWFpbigpIHsKICBjb3V0IDw8ICJBc3NpZ25lZCBpbiBjb25zdHJ1Y3RvciIgPDwgZW5kbDsKICBCYXIgYmFyID0gQmFyKEZvbyg1KSk7CiAgY291dCA8PCAiQXNzaWduZWQgaW4gaW5pdGlhbGl6ZXIgbGlzdCIgPDwgZW5kbDsKICBCYXIgYmFyMiA9IEJhcihmYWxzZSwgRm9vKDUpKTsKfQ==