小编Sch*_*ann的帖子

Delphi XE:类构造函数不会在使用泛型的类中调用

考虑以下示例(我使用的是Delphi XE):

program Test;

{$APPTYPE CONSOLE}

type
  TTestClass<T> = class
  private
    class constructor CreateClass();
  public
    constructor Create();
  end;

class constructor TTestClass<T>.CreateClass();
begin
  // class constructor is not called. this line never gets executed!
  Writeln('class created');
end;

constructor TTestClass<T>.Create();
begin
  // this line, of course, is printed
  Writeln('instance created');
end;

var
  test: TTestClass<Integer>;

begin
  test := TTestClass<Integer>.Create();
  test.Free();
end.
Run Code Online (Sandbox Code Playgroud)

从不调用类构造函数,因此不会打印"创建类"行.但是,如果我删除泛化并TTestClass<T>进入标准类TTestClass,一切都按预期工作.

我是否遗漏了仿制药?或者它根本不起作用?

对此的任何想法都会受到关注!

谢谢, - 斯特凡 -

delphi generics class-constructors delphi-xe

11
推荐指数
2
解决办法
2011
查看次数

标签 统计

class-constructors ×1

delphi ×1

delphi-xe ×1

generics ×1