纯虚拟调用从构造函数和析构函数 [英] Pure virtual invocation from constructor and destructor
问题描述
C ++标准说,禁止从构造函数或析构函数调用纯虚函数。这是什么原因?为什么标准应该这样限制?
The C++ standard says that invoking a pure virtual function from a constructor or destructor is forbidden. What is the reason for this? Why should the standard place a restriction like this?
推荐答案
当一个类析构函数运行时,所有子类析构函数都有已经运行。调用由子类定义的虚方法是无效的,它的析构函数已经运行了。
At the point a class destructor is run, all subclass destructors have already been run. It would not be valid to call a virtual method defined by a subclass, for which its destructor has already run.
在构造函数中调用虚方法时存在类似的限制。您不能为其构造函数尚未运行的子类调用虚拟方法。
A similar restriction exists around calling virtual methods in constructors. You can't call a virtual method for a subclass whose constructor has not yet run.
这篇关于纯虚拟调用从构造函数和析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!