原文链接:

OOP50-CPP. Do not invoke virtual functions from constructors or destructors


虚函数允许在运行时根据被调用的成员函数所属对象的动态类型来决定选取调用的成员函数。这一惯例支持与对象继承和函数重载常联系在一起的面向对象编程实践。当调用非虚成员函数或者当使用类成员访问表达式指明调用的,则指定的函数将被调用。否则,对象的动态类型最终重载函数虚函数将被调用。

然而,在对象的构造和析构过程中,对象上虚方法调度规则是有限制的。C++ 标准, [class.cdtor], 段落 4 [ISO/IEC 14882-2014], 陈述如下:

Member functions, including virtual functions, can be called during construction or destruction. When a virtual function is called directly or indirectly from a constructor or from a destructor, including during the construction or destruction of the class’s non-static data members, and the object to which the call applies is the object (call it x) under construction or destruction, the function called is the final overrider in the constructor’s or destructor’s class and not one overriding it in a more-derived class. If the virtual function call uses an explicit class member access and the object expression refers to the complete object of x or one of that object’s base class subobjects but not x or one of its base class subobjects, the behavior is undefined.

不要从试图构造和析构对象的构造和析构函数中直接或间接调用虚函数。因为构造函数的顺序从基类开始到派生类,所以试图从基类构造过程调用派生类的函数是危险的。派生类并没有机会初始化自身资源,这就是为什么从构造函数调用虚函数并不会调用到派生类的函数。同样地,一个对象销毁顺序和构造相反,所以试图在派生类调用这类函数可能会访问到早已经释放的资源。