Edit

Share via


Compiler Warning C5038

data member 'member1' will be initialized after data member 'member2'
data member 'member' will be initialized after base class 'base_class'

Remarks

Class members get initialized in the order they're declared, not the order they appear in initializer lists. The compiler warns when the initialization order isn't the same as the declaration order of data members or base classes. The order can lead to undefined runtime behavior: for example, if the initialization of one member in the list depends on the initialization of a member that's declared later.

This warning is new in Visual Studio 2017 version 15.3, and is off by default. Use /Wall to enable all warnings that are off by default, or /w15038 to enable C5038 as a level 1 warning. For more information, see Compiler warnings that are off by default. For information on how to disable warnings by compiler version, see Compiler warnings by compiler version.

Example

In the following example, The compiler (when using /w15038) raises "warning C5038: data member 'A::y' will be initialized after data member 'A::x'":

// C5038.cpp
// Compile using: cl /EHsc /c /w15038 C5038.cpp
struct A
{
    A(int a) : y(a), x(y) {} // C5038, Initialized in reverse, y reused
    int x;
    int y;
};

To fix this issue, arrange the initializer list to have the same order as the declarations. A similar warning is raised when one or both initializers refer to base class members.