Sch*_*ann 11 delphi generics class-constructors 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,一切都按预期工作.
我是否遗漏了仿制药?或者它根本不起作用?
对此的任何想法都会受到关注!
谢谢, - 斯特凡 -
看起来像编译器错误.如果将TTestClass声明和实现移动到单独的单元,则相同的代码可用.
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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2011 次 |
| 最近记录: |