【发布时间】:2011-07-23 19:27:54
【问题描述】:
According to the Delphi docs,我可以使用as 运算符将TInterfacedObject 转换为接口。
但这对我不起作用。强制转换给出编译错误:“运算符不适用于此操作数类型”。
我使用的是 Delphi 2007。
这是一些代码(控制台应用程序)。包含错误的行被标记。
program Project6;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
IMyInterface = interface
procedure Foo;
end;
TMyInterfacedObject = class(TInterfacedObject, IMyInterface)
public
procedure Foo;
end;
procedure TMyInterfacedObject.Foo;
begin
;
end;
var
o: TInterfacedObject;
i: IMyInterface;
begin
try
o := TMyInterfacedObject.Create;
i := o as IMyInterface; // <--- [DCC Error] Project6.dpr(30): E2015 Operator not applicable to this operand type
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.
【问题讨论】:
-
docwiki 是否正确?链接页面中的第一句话是“实现接口的类可以使用 as 运算符对接口进行动态绑定。”。
标签: delphi interface delphi-2007