function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall; //说明(3)
function _AddRef: Integer; stdcall; //使接口引用数加1。
function _Release: Integer; stdcall;//使接口引用数减1,当小于等于0时作释放动作。
end;
function TIntfClass._Release: Integer;
begin
Result := InterlockedDecrement(FRefCount);
end;
5、接口的委托(Interface Delegation)
分为两种:
1. 对象接口委托
2. 类对象委托。
. 对象接口委托,假如已有下面接口定义:
IImplInterface = interface(IInterface)
function ConvertToUSD(const iNTD: Integer): Double;
function ConvertToRMB(const iNTD: Integer): Double;
end;
接着有一个类实现了该接口:
TImplClass = class(TObject, IImplInterface)
private
FRefCount: Integer;
public
function ConvertToUSD(const iNTD: Integer): Double;
...
end;
implementation
function TImplClass.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
function TImplClass._Release: Integer;
begin
Result := InterlockedDecrement(FRefCount);
if Result = 0 then
Destroy;
end;
... ...