VCL里的构造函数
好奇,为什么Create函数明明是个构造函数,还要带上override;这是C++里没有的事情。我虽然也明白其大致的作用和目的,但还是没有见到官方和权威的说法。如果哪位大大见到此文,还望给一个详细一点的解释,谢谢。
----------------------------VCL关键类-------------------
TObject = class
constructor Create; // 注意不带参数。可能虚构造函数只对某一个起作用,而不是全体
destructor Destroy; virtual;
TPersistent = class(TObject)
没有构造函数和析构函数
TComponent = class(TPersistent, IInterface, IInterfaceComponentReference)
constructor Create(AOwner: TComponent); virtual;
destructor Destroy; override;
TControl = class(TComponent)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
TWinControl = class(TControl)
constructor Create(AOwner: TComponent); override;
constructor CreateParented(ParentWindow: HWnd); // 注意,这里还不是overload,因为构造函数的名称不同
destructor Destroy; override;
TGraphicControl = class(TControl)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
TCustomControl = class(TWinControl)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
----------------------------具体的控件类-------------------
TCustomLabel = class(TGraphicControl)
constructor Create(AOwner: TComponent); override; // 统统都有覆盖,应该是为了方便TControlClass来创建类(不是类对象)
没有析构函数
TLabel = class(TCustomLabel)
没有构造函数和析构函数,只发布属性
TCustomEdit = class(TWinControl)
constructor Create(AOwner: TComponent); override;
TEdit = class(TCustomEdit)
没有构造函数和析构函数,只发布属性
TButtonControl = class(TWinControl)
constructor Create(AOwner: TComponent); override;
没有析构函数
TCustomButton = class(TButtonControl)
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
TButton = class(TCustomButton)
没有构造函数和析构函数,只发布属性
----------------------------总结-------------------
析构函数从TObject开始就是虚函数,因为每个类都要释放内存和其它资源,方便覆盖并使用。
构造函数从TComponent开始是虚函数,所有继承类只要是使用这个Create(AOwner: TComponent)这个函数都要写上override,猜测目的是方便元类(类引用、类之类)正确调用其构造函数。但对直接继承TObject的类没有这个要求。
TPersistent没有构造以及析构函数,不知道其调用情况是怎么样的。有空改写VCL库做一下测试。
----------------------------问题-------------------
Delphi的函数修饰关键字过多、过于强大和精确,以至于很难精确掌握其使用。Java所有函数全部都是虚函数、可重载、可覆盖。C++重载不需要程序员管,覆盖也是自动的,更没有reintroduce,缺点是我发现一旦覆盖父类的某一个函数,那么会把父类所有的同名但不同参数的函数都会藏起来,这样的默认陷阱不知道还有多少。不像Delphi可以精确控制,但学起来也挺麻烦。
virtual - Mark this as a function where you will want run-time dispatching (allows polymorphic behavior). This is only for the initial definition, not when overriding in subclasses.
override - Provide a new implementation for a virtual method.
overload - Mark a function with the same name as another function, but a different parameter list.
reintroduce - Tell the compiler you actually intended to hide a virtual method, instead of merely forgetting to supply override.
原理明白了,但是有几个例子无法搞清,有功夫再回来研究:
http://stackoverflow.com/questions/3876040/delphi-understanding-constructors
http://stackoverflow.com/questions/3874330/delphi-how-to-hide-ancestor-constructors
官方参考:
http://docwiki.embarcadero.com/RADStudio/XE6/en/Methods
最新文章
- Linux中文件颜色所代表的属性和颜色
- 处理mysql上百万条数据的优化
- c++primerplus(第六版)编程题——第4章(复合类型)
- if 和 swith的选择.
- Linux Kernel ‘kvm_set_memory_region()’函数本地提权漏洞
- Java基础IO文件拷贝练习题
- Openlayer 3 最简单的弹出框
- 修改ElementUI源码实践
- 【C语言编程练习】5.10寻找水仙数
- ABAP on HANA之CDS Association和Path Expression
- 【读书笔记】iOS-设计模式
- 完全二叉树的节点个数 Count Complete Tree Nodes
- 8个纯CSS3制作的动画应用及源码
- Android网络编程系列之HTTP协议原理总结
- bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)
- 强网杯2018 pwn复现
- C语言中const有什么用途
- STM32开发(一):简介及开发环境
- Struts2知识点小结汇总
- POJ-2155:Matrix(二维树状数祖)
热门文章
- PatentTips - MPLS Network System
- os.system() 和 os.popen()
- 利用例子来理解spring的面向切面编程(使用@Aspect)
- Java Web工程连接MySQL数据库及Tomcat服务器页面中文乱码
- DELPHI跨平台编译开关
- openfire Android 学习(四)----单人聊天和多人聊天(发送消息、接收消息)
- Java并发学习 &; Executor学习 &; 异常逃逸 &; 同步互斥Best Practice &; wait/notify, conditon#await/signal
- HDU 5304(Eastest Magical Day Seep Group&;#39;s Summer-环加外向树生成树计数)[Template:Kirchhoff矩阵]
- 近期微信上非常火的小游戏【壹秒】android版——开发分享
- 一起talk C栗子吧(第八十四回:C语言实例--使用信号进行进程间通信一)