Sign in to follow this  
mychii

C++ class properties initialization order on constructor?

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this