Is assigning m_B to m_A in the constructor below legal, provided that m_B is declared after m_A?
class C {
int m_A, m_B;
C() : m_A(0), m_B(m_A) {}
};
Is assigning m_B to m_A in the constructor below legal, provided that m_B is declared after m_A?
class C {
int m_A, m_B;
C() : m_A(0), m_B(m_A) {}
};
Yes, it is legal to assign a member to another member in a constructor. There is an example in the standard (§ 15.6.2/15 )
class X { int a; int b; int i; int j; public: const int& r; X(int i): r(a), b(i), i(i), j(this->i) { } };
Note that the data members are initialized in the order they are declared in the class definition. The order of the member initializers doesn't matter. So, the below is valid
class C {
int m_A, m_B;
C() : m_B(m_A), m_A(0) {} // Swapped the member initialization order.
};
but this isn't valid
class C {
int m_B, m_A; // Swapped the declaration order.
C() : m_B(m_A), m_A(0) {}
};