Public Group

# C++ class properties initialization order on constructor?

This topic is 694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys, I somehow got into this problem but I don't know why. Say I have a simple code like this:

class A
{
public:
int a;
int b;
int c;

A() :
b(1),
a(1),
c(2)
{}
};

int main()
{
A a;



Using VS 2015 debugger, I put down a breakpoint on A a; and trace it down. Somehow it shows that 'a' is initialized first, then 'b'. I was thinking that 'b' should be initialized first then 'a'. My assumption is that this happens because the first declared property is 'int a' instead of 'int b', because when I switched them, it works by order (b, a, then c).

The big question to clear my assumptions is : why? :huh:

##### Share on other sites

Alllright thanks for the explanation Lactose! ^_^

Yes I've tried that works just fine in constructor body. I was just curious on that one that I thought it works just the same. :P

Edited by Alectora

##### Share on other sites

Doesn't Microsoft's compiler warn you about such things? I know GCC and clang do...

##### Share on other sites

It might depends on the warning level he choose for the compilation.

Anyway, with C++11, you can set the value of the members declarations:

class A
{
int a = 0;
int b = 1;
};

Edited by _Silence_

##### Share on other sites

Doesn't Microsoft's compiler warn you about such things? I know GCC and clang do...

Funny enough it didn't. Maybe I should turn on something more strict like _Silence_ said.

It might depends on the warning level he choose for the compilation.
Anyway, with C++11, you can set the value of the members declarations:

class A
{
int a = 0;
int b = 1;
};


I'll try that thanks for the useful info. Gonna check the compilation level, not sure what it is currently. :p

##### Share on other sites

Welps I was at /W3. Tried /W4 and it doesn't show anything related to the issue. I tried /Wall, it hit windows library as well so I guess that's too far (even though they aren't warnings, but more like just info what it is doing). It's not critical, but I'll be setting up the Android environment with clang later so I'll be hitting that warning soon maybe.

Edited by Alectora

##### Share on other sites

MS compiler doesn't warn on this, it's very annoying...

##### Share on other sites

The language standard states that they must be in increasing order, which means they match the order they appear in the file.

Specifically, "When an aggregate is initialized by an initializer list, as specified in 8.5.4, the elements of the initializer list are taken as initializers for the members of the aggregate, in increasing subscript or member order."

Some compilers handle it in different order, but that's a compiler variance that is usually documented.  Visual C++ does not provide a warning for this, and has said multiple times (such as here) that they are thinking about it, but they never actually implement the warning.

##### Share on other sites
So it's already an old issue.. Good to know that they said it is (finally) under review though.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001962
×