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

后端 未结 2 1617
挽巷
挽巷 2020-12-16 17:55

Consider the following example (I am using Delphi XE):

program Test;

{$APPTYPE CONSOLE}

type
  TTestClass = class
  private
    class constructor          


        
相关标签:
2条回答
  • 2020-12-16 18:06

    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.

    0 讨论(0)
  • 2020-12-16 18:09

    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.
    
    0 讨论(0)
提交回复
热议问题