Friday, December 3, 2010

The Order of the Initialization of Member Variables

    int g_a;
    class A {public: A(int a) : m_a(a) {} int m_a;};
    class B {public: B(): m_a2(++g_a), m_a1(++g_a) {} A m_a1; A m_a2;};
    int main() {B b; return b.m_a2.m_a;}

The return code of this program is 2.

The order of the initialization of member variables is based on the order of their declaration and not on the order that they are listed in the MIL (Member Initialization List), which is the part of the constructor between the ":" and "{".

No comments:

Post a Comment