Execution order of constructor initialization list with delegated constructors

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

There are two conflicting standard rules:
1.) The initialization list of a constructor is started NOT in the order of the list, but in the order in which the elements are declared.
2.) Delegated constructors in the constructor initialization list are always called before the "mothers constructor" is executed.

Which rule is superior? (since a constructor is also a member of a class) Why this matters: Suppose a delegated constructor re-introduces an element already initialized by the "parent constructor", or vice versa.

+3


source to share


1 answer


ยง12.6.2 / 6 says

If mem-initializer-id denotes a constructor class, it must be the only mem-initializer ... After the target constructor returns, the body of the delegating constructor.



This way there is no conflict as you cannot initialize anything before delegating the constructor. A constructor delegation simply calls that constructor, the target constructor's initializer list is run, the target constructor is run, and then the main constructor is run.

+2









All Articles