构造函数不能虚,多态基类析构函数应为虚

构造函数不能为虚

由于派生类和基类之间的关系是一种IS-A的关系,所以通常用基类指针或引用指向派生类对象。
为了构造对象,constructor必须要事先知道对象的确切类型,所以constructor不能为virtual.

首先要知道构造函数是用来干嘛的?

对于基类(when base is instantiated):

  1. 分配一块内存
  2. 调用构造函数
  3. 初始化列表初始化变量
  4. 执行构造函数函数体部分
  5. 控制权返回至调用者

对于派生类(when derived is instantiated):

  1. 分配内存(包括派生类对象的基类部分和派生类部分)
  2. 调用派生类构造函数
  3. 使用基类构造函数构造基类部分
  4. 初始化列表初始化变量
  5. 执行构造函数函数体部分
  6. 控制权返回给调用者

那么为什么构造函数不能为虚?

假设构造函数为虚,看看会发生什么!
如果一个类具有虚函数,那么在它的对象空间中就会有(编译器为基类添加它,继承类继承它)一个隐藏指针指向该类的虚函数表(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中找到合适的函数指针

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值