Effective c++ 条款9 绝不在构造和析构过程中调用virtual函数
作者:互联网
- 问题:
- 当derived class对象调用derived class constructor函数前,会先调用base class constructor函数。如果derived class重写了base class的虚函数vfunc(),base class constructor函数中调用vfunc(),此时derived class对象调用base class constructor,会调用base class版本的vfunc函数
- 原因:
- base class constructor函数比derived class constructor函数执行更早,当base class constructor函数执行时derived class的成员变量尚未初始化
- derived class对象的base class 构造期间,对象类型为base class,而不是derived class。并且dynamic_case、typeid(runtime type information)也把对象视为base class类型
- 析构函数同上
标签:调用,函数,Effective,derived,c++,base,virtual,constructor,class 来源: https://www.cnblogs.com/chenglixue/p/16391727.html