Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constructor initialization list execution order with delegated constructors

I have a tricky C++ question: When you have a constructor initialization list with delegated constructors, what is the list execution order?

There exist two conflicting standard rules here:
1.) The constructor initialization list gets executed NOT by the list order but by the declaration order of the items.
2.) Delegated constructors in the constructor initialization list always get called before the "mother constructor" is executed.

Which rule is superior? (since a constructor is a class item too) Why this is important: assume the delegated constructor re-inits an item already initialized by the "mother constructor" or vice versa.

like image 584
B M Avatar asked Oct 21 '22 17:10

B M


1 Answers

§12.6.2/6 says

If a mem-initializer-id designates the constructor’s class, it shall be the only mem-initializer... Once the target constructor returns, the body of the delegating constructor is executed.

So there's no conflict, since you can't initialise anything before you delegate a constructor. Delegating a constructor simply calls that constructor, the target constructor's initialiser list is run, the target constructor runs, and then the principal constructor runs.

like image 64
Seth Carnegie Avatar answered Oct 27 '22 10:10

Seth Carnegie