zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    [Tips]git cannot lock ref
    [BUG]Git Sever搭建与相关错误处理
    [Tips]matplotlib 命令行画图保存
    [Tips]Torch功能点记录
    5G元年教育产业再出发 科技赋能的“风口与风险”仍待明晰
    Adobe逆天AI黑科技:美颜照克星,秒还原PS照片
    计算机算法能听声音绘制人脸
    2019 CESA,智能座舱越来越“懂你”
    谷歌首席决策科学家:AI难免犯错,唯有人类可以悬崖勒马
    百度:如何将AI进行到底?
  • 原文地址:https://www.cnblogs.com/findumars/p/3673656.html
Copyright © 2011-2022 走看看