构造函数不能为虚
由于派生类和基类之间的关系是一种IS-A的关系,所以通常用基类指针或引用指向派生类对象。
为了构造对象,constructor必须要事先知道对象的确切类型,所以constructor不能为virtual.
首先要知道构造函数是用来干嘛的?
对于基类(when base is instantiated):
- 分配一块内存
- 调用构造函数
- 初始化列表初始化变量
- 执行构造函数函数体部分
- 控制权返回至调用者
对于派生类(when derived is instantiated):
- 分配内存(包括派生类对象的基类部分和派生类部分)
- 调用派生类构造函数
- 使用基类构造函数构造基类部分
- 初始化列表初始化变量
- 执行构造函数函数体部分
- 控制权返回给调用者
那么为什么构造函数不能为虚?
假设构造函数为虚,看看会发生什么!
如果一个类具有虚函数,那么在它的对象空间中就会有(编译器为基类添加它,继承类继承它)一个隐藏指针指向该类的虚函数表(The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. This table is simply a static array that the compiler sets up at compile time. First, every class that uses virtual functions is given its own virtual table. Second, the compiler also adds a hidden pointer to the base class, which we will call *__vptr. *__vptr is set (automatically) when a class instance is created so that it points to the virtual table for that class. When a class object is created, *__vptr is set to point to the virtual table for that class.),假设构造函数为虚函数,那么就需要通过虚函数表来调用应该调用的函数(An object of type Base can only access the members of Base. D has overridden function(), making D::function() more derived than Base::function().), 这时候问题来了,刚刚在第一步分配的内存还没初始化,所以根本还没有虚函数表呢!所以构造函数不能为虚!
多态基类的析构函数应该为虚
何时调用析构函数?
简单地说,The destructor is called when an object is destroyed,具体分为两种情况:生存周期正常结束(goes out its scope normally)或者通过delete显式删除。
还是那句话,由于派生类和基类之间的关系是一种IS-A的关系,所以基类指针或引用可以指向任何一个派生类对象。
当一个派生类对象通过基类指针或引用删除,且这个基类有一个非虚的析构函数,将不会调用整个析构链,结果会是未定义的!
当类中包含至少一个虚函数时,才将该类的虚函数声明为虚。
当一个对象调用虚函数时
找到对象的vptr指向的virtual table,在virtual table中找到合适的函数指针