Advertisement

C++ class properties initialization order on constructor?

Started by August 22, 2016 08:13 AM
8 comments, last by mychii 8 years, 3 months ago

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:

Initializer lists are done in the order of the declarations, not in the order you put them in the initializer list.

If you need to initialize things out of order, you'll need to put it in the constructor body instead of in the initializer list.

Hello to all my stalkers.

Advertisement

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

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

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;
};

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
Advertisement

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.

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

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.

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

This topic is closed to new replies.

Advertisement