Never Call Virtual Functions during Constructio...
为什么80%的码农都做不了架构师?>>>
代码
1. java
class Bird {public Bird() {show();}public void show() {System.out.println("hey, I'm a bird.");}
}class Eagle extends Bird {private String name = "littleEgale";public Eagle(String name) {this.name = name;}@Overridepublic void show() {System.out.println("I'm a bald eagle, my name is " + name + '.');}}public class VirtualTester {public static void main(String[] args) {new Eagle("java");}}
2. c++
#include <iostream>
#include <string>
using namespace std;class Bird {
public:Bird() {show();}virtual void show() {cout << "hey, I'm a bird." << endl;}};class Eagle : public Bird {
private :string name;
public:Eagle(const string &name):name(name){}void show() {cout << "I'm a bald eagle, my name is " << name<< "." <<endl;}
};int main() {Eagle("cPlusPlus");
}
3. python
class Bird():def __init__(self):self.show()def show(self):print("hey, I'm a bird.")class Eagle(Bird):def __init__(self,name):self.__name = name # 1Bird.__init__(self) # 2def show(self):print("I'm a bald eagle, my name is",self.__name,'.')Eagle('python')
运行结果:
java: I'm a bald eagle, my name is null.
c++: hey, I'm a bird.
python: I'm a bald eagle, my name is python.
分析
Item 9:Never Call Virtual Functions during Construction or Destruction. Effective C++
Puzzle 49: Larger Than Life. Java Puzzlers
结论:
不在构造和析构过程中调用virtual函数, 适用于基类初始化限定在子类初始化之前的语言。 一个设计良好的继承体系,应该避免在基类构造函数中直接或间接的调用被子类重载的方法。
转载于:https://my.oschina.net/coda/blog/34971
Never Call Virtual Functions during Constructio...相关推荐
- delete called on non-final ‘xxx‘ that has virtual functions but non-virtual destructor
Qt中遇到如下警告 warning: delete called on non-final 'xxx' that has virtual functions but non-virtual destr ...
- because the following virtual functions are pure within
构建了包含纯虚函数的对象 包含纯虚函数的类不能实例化为对象
- [C++]Call virtual member function in constructor or destructor
嗷嗷按,今天被问到在constructor/destructor中调用virtual member function的问题.答错了,很羞耻. 依稀记得在constructor/destructor调用 ...
- Pure Virtual Function
Pure Virtual Function • If a base class has no natural implementation for a virtual function, then t ...
- Effective C++条款09:绝不在构造和析构过程中调用virtual函数
Effective C++条款09:绝不在构造和析构过程中调用virtual函数(Never call virtual functions during construction or destruc ...
- 转:Virtual Function I/O
VFIO - "Virtual Function I/O" [1] Many modern system now provide DMA and interrupt remappi ...
- c 语言的虚函数,C 中的虚函数(virtual function)
一.简介 虚函数是C++中用于实现多态(polymorphism)的机制.核心理念就是通过基类访问派生类定义的函数.假设我们有下面的类层次: class Father { public: virtua ...
- C++ 错误 pure virtual method called
C++ 错误 pure virtual method called 某日做UnitTest(单元测试)时,Gtest跑完析构时,报出如下错误 ..... ..... ..... [ PASSED ] ...
- 使用现代C++如何避免bugs(下)
使用现代C++如何避免bugs(下) About virtual functionsVirtual functions hinder a potential problem: the thing is ...
- LIVE555中RTSP客户端接收媒体流分析及测试代码
LIVE555中testProgs目录下的testRTSPClient.cpp代码用于测试接收RTSP URL指定的媒体流,向服务器端发送的命令包括:DESCRIBE.SETUP.PLAY.TERAD ...
最新文章
- spss相关性分析_SPSS有话说:如何得出问卷或量表的结构或维度——探索性因素分析...
- 考前自学系列·计算机组成原理·补码定点加减运算和溢出判断,浮点数的加减运算,原码的乘法
- 百度金融资深产品经理降峰:互金产品如何准确触达用户
- 计算机学报格式_如何快速写好一篇格式正确的参考文献?
- DEFINE_PER_CPU
- textureview 旋转90度后平铺_C++初级编程NOIP题:11H1537: 图像旋转
- lumen 下使用自定义配置文件
- 【Java从0到架构师】Spring - 整合 MyBatis
- Confluence wiki上传文件时报“不能上传文件至Confluence中,服务器可能已失效”
- [redis] redis连接远程客户端查询数据
- 【codeforces 417D】Cunning Gena
- java系统爬塔游戏,如何玩爬塔游戏?一目了然,每天只需2分钟
- DBeaver安装过程的问题各个击破
- github上如何删除自己的仓库
- 【MATLAB图像处理实用案例详解(1)】—— 基于直方图优化的图像去雾技术
- Single-stage目标检测网络YOLO相关背景知识
- Oracle JDBC使用lob不释放临时表空间的解决方案
- Kong 插件ACL的使用方法(访问控制列表黑名单)
- 2022最新彩虹易支付系统源码原版全开源源码
- vue实现数字动态翻牌器