Delphi XE: class constructor doesn't get called in a class using generics

本小妞迷上赌 提交于 2019-11-29 03:56:05

Looks like a compiler bug. The same code works if you move the TTestClass declaration and implementation to a separate unit.

unit TestClass;

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

var
  test: TTestClass<Integer>;

implementation

class constructor TTestClass<T>.CreateClass();
begin
  Writeln('class created');
end;

constructor TTestClass<T>.Create();
begin
  Writeln('instance created');
end;

end.

I can confirm that this is a bug. If the only instantiation of the class is in the .dpr file, then the class constructor does not run. If you create another unit, i.e. a separate .pas file, and instantiate a TTestClass<Integer> from there, then your class constructor will run.

I have submitted QC#103798.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!