Delphi/Pascal:使用其他原型重载构造函数 [英] Delphi/pascal: overloading a constructor with a different prototype

查看:120
本文介绍了Delphi/Pascal:使用其他原型重载构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

  1. 在某些情况下的特殊构造函数,并且
  2. 将与当前代码保持兼容性的默认构造函数.

这是我现在拥有的代码:

This is the code I have now:

interface
  TfrmEndoscopistSearch = class(TForm)
  public
    /// original constructor kept for compatibility
    constructor Create(AOwner : TComponent); overload; override;
    /// additional constructor allows for a caller-defined base data set
    constructor Create(AOwner : TComponent; ADataSet : TDataSet; ACaption : string = ''); overload;
  end;

这似乎可行,但我总是收到编译器警告:

It seems to work, but I always get the compiler warning:


[Warning] test.pas(44): Method 'Create' hides virtual method of base type 'TCustomForm'

  • 添加超载;"在第二个构造函数之后将无法编译. "[错误] test.pas(44):创建"的声明与之前的声明不同."
  • 使第二个构造函数成为类函数,编译时不会出现任何错误或警告,但会在运行时死于访问冲突(所有成员var均为nil).
  • 推荐答案

    尝试在第二个overload之前添加reintroduce,如下所示:

    Try adding reintroduce before the second overload, like this:

      TfrmEndoscopistSearch = class(TForm)
      public
        /// original constructor kept for compatibility
        constructor Create(AOwner : TComponent); overload; override;
        /// additional constructor allows for a caller-defined base data set
        constructor Create(AOwner : TComponent; ADataSet : TDataSet; ACaption : string = ''); reintroduce; overload;
      end;
    

    这是在Turbo Delphi中编译的.我需要public进行编译,因为published方法的重载受到限制.

    This compiles in Turbo Delphi. I needed the public to make it compile because overloading of published methods is restricted.

    这篇关于Delphi/Pascal:使用其他原型重载构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆