Delphi 抽象函数调用的名称与基类不同
假设我有一个函数
function GetFieldName(FieldIndex: Integer; FieldName: PChar;
Len: Integer): Integer; virtual; abstract;
,并且我正在尝试添加
function GetFieldNameA(FieldIndex: Integer; FieldName: PAnsiChar;
Len: Integer): Integer;
它将暂时桥接我的 unicode 数据库连接。
我想继续调用 GetFieldName,使其不抽象,并进行一些位类型转换来调用 GetFieldNameA,这将成为第一个 GetFieldName 的技术抽象版本。 (我根本不想更改基类)
是否有一种方法,例如为外部引用添加“name”关键字,可以在子类中使用具有不同名称的抽象函数?
我想象的最终结果是这样的:
function GetFieldName(FieldIndex: Integer; FieldName: PChar;
Len: Integer): Integer;
function GetFieldNameA(FieldIndex: Integer; FieldName: PAnsiChar;
Len: Integer): Integer name 'GetFieldName Virtual Abstract';
Say I've got a function
function GetFieldName(FieldIndex: Integer; FieldName: PChar;
Len: Integer): Integer; virtual; abstract;
and I'm trying to add
function GetFieldNameA(FieldIndex: Integer; FieldName: PAnsiChar;
Len: Integer): Integer;
Which will temporarily bridge my database connection for unicode.
I want to continue to call GetFieldName, make it not abstract, and do some bit typecasting to call GetFieldNameA, which will become a technically abstract version of the first GetFieldName. (I don't want to change the base class at all)
Is there a way, like adding the 'name' keyword for external references, to have an abstract function with a different name in the subclass?
What I'm imagining ending up with is something like:
function GetFieldName(FieldIndex: Integer; FieldName: PChar;
Len: Integer): Integer;
function GetFieldNameA(FieldIndex: Integer; FieldName: PAnsiChar;
Len: Integer): Integer name 'GetFieldName Virtual Abstract';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不能按照你的建议去做。
相反,请覆盖
GetFieldName
。在调用GetFieldNameA
之前,将PChar
转换为PAnsiChar
(如有必要)。后者不必(实际上也不能)被标记为覆盖
。它可以是普通的非虚函数。该建议的不幸部分是您必须在每个后代类中执行此操作。另一种方法是向基类添加虚拟抽象
GetFieldNameA
,然后更改所有后代以覆盖它,而不是GetFieldName
。更改基类中的GetFieldName
以调用GetFieldNameA
。但如果你不能改变基类,那是行不通的。No, you can't do what you're proposing.
Instead, override
GetFieldName
. Have it convert thePChar
to aPAnsiChar
(if necessary) before callingGetFieldNameA
. The latter doesn't have to be (and can't, actually) be markedoverride
. It can be an ordinary non-virtual function.The unfortunate part of that suggestion is that you'd have to do it in every descendant class. The alternative is to add a virtual abstract
GetFieldNameA
to the base class and then change all the descendants to override that instead ofGetFieldName
. ChangeGetFieldName
in the base class to callGetFieldNameA
. But that's a non-starter if you can't change the base class.如果我正确理解你的问题,你可以通过注入替换的基类来欺骗 Delphi。
如果声明原始 GetFieldName 的类称为在 Unit1(pas 或 dcu)中声明的 TClass1,则:
类型TClass1 = class(Unit1.TClass1)
只要 TrickUnit.pas 位于使用部分的末尾,您的所有后代都会结束继承被替换的类,除了添加
using ......, TrickUnit
之外,无需更改任何代码If I understood your question correctly you can trick Delphi by injecting replaced base class.
If the class that declares original GetFieldName is called TClass1 declared in Unit1(pas or dcu), then:
type TClass1 = class(Unit1.TClass1)
As long as TrickUnit.pas is at the end of uses section, all of your descendants will end up inheriting your replaced class with no code changes other than adding
using ......, TrickUnit