https://www.bilibili.com/video/BV1Kb411B7N8
和普通函数对比
首先,C++不支持虚拟构造函数。是有构造函数特点决定的
关于C++为什么不支持虚拟构造函数,Bjarne很早以前就在C++
Style and Technique
FAQ里面做过回答:A
virtual call is a mechanism to get work done given partial
information.
In particular, "virtual" allows us to call a
function knowing only an interfaces and not the exact type of the
object.
To create an object you need complete information. In
particular, you need to know the exact type of what you want to
create. Consequently, a "call to a constructor" cannot be
virtual.出处:Stroustrup: C++ Style and Technique FAQ
作者:左轻侯
链接:https://www.zhihu.com/question/35632207/answer/63936329
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
不懂 构造函数无法通过指针访问原理,new 如何调用类构造函数申请的用法。
抽象和具体关系.接口不属于具体一个类
参考思路:
如果析构函数不被声明成虚函数,则编译器实施静态绑定,在删除指向派生类的基类指针时,只会调用基类的析构函数而不调用派生类析构函数,这样就会造成派生类对象析构不完全。
如果声明了,触发 派生类 ,基类 正确析构顺序。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有