c++initialization-listmember-variables

Can member variables be used to initialize other members in an initialization list?


Consider the following (simplified) situation:

class Foo
{
private:
    int evenA;
    int evenB;
    int evenSum;
public:
    Foo(int a, int b) : evenA(a-(a%2)), evenB(b-(b%2)), evenSum(evenA+evenB)
    {
    }
};

When i instanciate Foo like this:

Foo foo(1,3);

then evenA is 0, evenB is 2, but will evenSum be initialized to 2?

I tried this on my current platform (iOS) and it seems to work, but I'm not sure whether this code is portable.

Thanks for your help!


Solution

  • This is well-defined and portable,1 but it's potentially error-prone.

    Members are initialized in the order they're declared in the class body, not the order they're listed in the initialization list. So if you change the class body, this code may silently fail (although many compilers will spot this and emit a warning).


    1. From [class.base.init] in the C++ standard(s):

    In a non-delegating constructor, initialization proceeds in the following order:

    • First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
    • Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
    • Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
    • Finally, the compound-statement of the constructor body is executed.

    (Highlighting is mine.)

    This section of the standard then goes on to give an example of using member variables to initialize other member variables.