order of destruction using virtual

前端 未结 8 1279
伪装坚强ぢ
伪装坚强ぢ 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.

提交回复
热议问题