我需要一个没有引用计数的类实现接口。我做了以下事情:
IMyInterface = interface(IInterface)
['{B84904DF-9E8A-46E0-98E4-498BF03C2819}']
procedure InterfaceMethod;
end;
TMyClass = class(TObject, IMyInterface)
protected
function _AddRef: Integer;stdcall;
function _Release: Integer;stdcall;
function QueryInterface(const IID: TGUID; out Obj): HResult;stdcall;
public
procedure InterfaceMethod;
end;
procedure TMyClass.InterfaceMethod;
begin
ShowMessage('The Method');
end;
function TMyClass.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
function TMyClass._AddRef: Integer;
begin
Result := -1;
end;
function TMyClass._Release: Integer;
begin
Result := -1;
end;
缺少引用计数可以正常工作。但我担心的是我不能TMyClass
使用IMyInterface
usingas
运算符:
var
MyI: IMyInterface;
begin
MyI := TMyClass.Create as IMyInterface;
我被给
[DCC 错误] E2015 运算符不适用于此操作数类型
当TMyClass
源自TInterfacedObject
- 即我可以在没有编译器错误的情况下进行此类转换时,问题就会消失。显然我不想使用 TInterfacedObject 作为基类,因为它会使我的类引用计数。为什么不允许这种铸造以及如何解决它?