4
class A
{
  public:
    int a ,b;
    A() : a(1)
    {
      b=3;
    }
};

If we create an object of this class:

A obj;

then which one will be initialized first, a or b?

In the process of assigning b = 3 will there be any involvement of default constructor? I'm referring to the answer provided: If you use assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.

Community
  • 1
  • 1
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • @Barry I don't think that duplicate make much sense since it does not address the issue of the constructor body and default initialization which is key to this question and does not play at all in the duplicate you mentioned. I did not find anything obviously a duplicate of this which is why I did not answer immediately b/c I thought I would find something. – Shafik Yaghmour Jul 07 '15 at 18:27

2 Answers2

3

mem-initializer are performed first in the order they were declared in the class definition and then the body of the constructor is executed.

For reference the draft C++ standard says:

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

[...]

  • 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

If you did not assign a value to b in the body of the constructor it would have an indeterminate value.

To clarify from the answer you mentioned, the part of the answer you seem to be referring to is:

if you use assignment then the fields will be first initialized with default constructors and then reassigned (via assignment operator) with actual values.

what they meant to say was that it would be default initialized which in the case for an int means an indeterminate value, from the draft C++ standard:

If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (5.17).

Note that using an indeterminate value is undefined behavior.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
2

Initialization order is always the order that the variables are declared in, irrespective of the order of the mem-initializers. So a then b.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • do you mind answering my 2nd query (updated) – GorvGoyl Jul 07 '15 at 17:55
  • 1
    Members `a` and `b` are variables of type `int` which is built-in type. Therefore, they don't have any "default" constructor which is implicitly called if they are not initialised in the initialisation list. Variable `b` will not be initialised in initialisation list at all. – Jakiša Tomić Jul 07 '15 at 17:56
  • 1
    @JerryGoyal `b` will be default-initialized (which for `int` means that nothing will happen and it will have indeterminate value), and then it will be assigned `3`. – Barry Jul 07 '15 at 17:57
  • oh! I get it..it's not for primitives – GorvGoyl Jul 07 '15 at 18:00