order of destruction using virtual

前端 未结 8 1263
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 00:35

Can some one please help what the order of destruction is when I am using virtual functions. Does it start with the base class and then derived class?

相关标签:
8条回答
  • 2021-02-06 01:03

    Virtual functions make no difference to the order of destruction, virtual base classes, on the other hand, do.

    Without virtual base classes, derived classes are always destroyed before their base classes; this is the reverse order in which they are constructed.

    For the most derived class, virtual base classes are constructed first, before other base classes and before the most derived class itself. Destruction happens in the reverse order. This means that a virtual base may be destroyed after a class that derives virtually from it, if that class is not the most derived class being destroyed. This can never happen for direct base classes.

    0 讨论(0)
  • 2021-02-06 01:05

    Assuming you have correctly declared your destructor as virtual.

    Then destruction is done in the exact opposite order of construction.

    In General this will be:

    A) Start in the most derived class.
    B) Repeat the following recursively.

    1) Execute the destructor code.
    2) Execute the destructor of each member (in reverse order of creation)
    3) Execute the destructor of the parent class. (if more than one in reverse order of creation)

    If you use virtual inheritance though then things are slightly different as the order of base class construction is not the same as normal. BUT The order of destruction is ALWAYS the reverse of the order of construction.

    0 讨论(0)
  • 2021-02-06 01:14

    Order of destructions if from the bottom up. (from derived to base)

    Short answer: the exact opposite of the constructor order.

    Long answer: suppose the "most derived" class is D, meaning the actual object that was originally created was of class D, and that D inherits multiply (and non-virtually) from B1 and B2. The sub-object corresponding to most-derived class D runs first, followed by the dtors for its non-virtual base classes in reverse declaration-order. Thus the destructor order will be D, B2, B1. This rule is applied recursively; for example, if B1 inherits from B1a and B1b, and B2 inherits from B2a and B2b, the final order is D, B2, B2b, B2a, B1, B1b, B1a.

    See the c++ faq section 25

    0 讨论(0)
  • 2021-02-06 01:20

    The destruction order is the construction order backwards. I've recently made a small tool to display the construction order for any hierarchy. Look here:

    • Where is the "virtual" keyword necessary in a complex multiple inheritance hierarchy?

    In the diagrams, the nodes with the smaller numbers are constructed first and destructed last.

    0 讨论(0)
  • 2021-02-06 01:20

    Section 12.6.2/5:

    Initialization shall proceed in the following order:

    • First, and only for the constructor of the most derived class as described below, virtual base classes shall be 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 class names in the derived class base-specifier-list.
    • Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
    • Then, nonstatic data members shall be initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers). — Finally, the body of the constructor is executed.

    [Note: the declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. ]

    0 讨论(0)
  • 2021-02-06 01:23

    It is the opposite way as the constructors. So derived first.

    0 讨论(0)
提交回复
热议问题