constructor initialization list execution order with delegated constructors

给你一囗甜甜゛ 提交于 2019-12-10 14:57:09

问题


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.


回答1:


§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.



来源:https://stackoverflow.com/questions/14857825/constructor-initialization-list-execution-order-with-delegated-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!