c++ class member initialization order

  ISO+IEC+14882-2011-C++11标准.pdf 

12 Special member functions

    12.6.2 Initializing bases and members

    In a non-delegating constructor, initialization proceeds in the following order:
        — First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in
        the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes,
        where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
        — Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list
        (regardless of the order of the mem-initializers).
        — 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.
        [ Note: The declaration order is mandated to ensure that base and member subobjects are destroyed in the
        reverse order of initialization. — end note ]

referrence:

https://www.cnblogs.com/kangjianwei101/p/5221014.html

posted @ 2020-05-26 17:04  乘于时  阅读(239)  评论(0编辑  收藏  举报