#include <iostream>
using namespace std;
struct M1 {
int m_i;
M1 ( int i) : m_i( i) { cout << "ctor M1 " << m_i<< "\n " ; }
} ;
struct M2 {
int m_o;
M2 ( M1 & m) : m_o( m.m_i ) { cout << "ctor M2 " << m_o<< "\n " ; }
} ;
class A {
M1 m1;
M2 m2;
public :
A( int i) : m2( m1) , m1( i) { cout << "ctor A\n " ; }
} ;
class B {
M2 m2;
M1 m1;
public :
B( int i) : m1( i) , m2( m1) { cout << "ctor B\n " ; }
} ;
int main( ) {
cout << "If members are declared in the right order: the initialisation is as expected\n " ;
A( 1 ) ;
cout << "If members are declared in the wrong order: the dependent element is constructed based on wrong value\n " ;
B( 2 ) ;
// your code goes here
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IE0xIHsKaW50IG1faTsgCk0xIChpbnQgaSkgOiBtX2koaSkgeyBjb3V0PDwiY3RvciBNMSAiPDxtX2k8PCJcbiI7IH0KfTsKCnN0cnVjdCBNMiB7CmludCBtX287IAkKTTIgKE0xICZtKSA6bV9vKG0ubV9pKSB7IGNvdXQ8PCJjdG9yIE0yICI8PG1fbzw8IlxuIjsgfQp9OwoKY2xhc3MgQSB7CglNMSBtMTsgCglNMiBtMjsgCnB1YmxpYzogCglBKGludCBpKSA6IG0yKG0xKSwgbTEoaSkgeyBjb3V0IDw8ICJjdG9yIEFcbiI7IH0KfTsKY2xhc3MgQiB7CglNMiBtMjsgCglNMSBtMTsgCnB1YmxpYzogCglCKGludCBpKSA6IG0xKGkpLCBtMihtMSkgIHsgY291dCA8PCAiY3RvciBCXG4iOyB9Cn07CmludCBtYWluKCkgewoJY291dCA8PCAiSWYgbWVtYmVycyBhcmUgZGVjbGFyZWQgaW4gdGhlIHJpZ2h0IG9yZGVyOiB0aGUgaW5pdGlhbGlzYXRpb24gaXMgYXMgZXhwZWN0ZWRcbiI7CglBKDEpOyAKCWNvdXQgPDwgIklmIG1lbWJlcnMgYXJlIGRlY2xhcmVkIGluIHRoZSB3cm9uZyBvcmRlcjogdGhlIGRlcGVuZGVudCBlbGVtZW50IGlzIGNvbnN0cnVjdGVkIGJhc2VkIG9uIHdyb25nIHZhbHVlXG4iOwoJQigyKTsgCgkvLyB5b3VyIGNvZGUgZ29lcyBoZXJlCglyZXR1cm4gMDsKfQ==