C++ Builder 參考手冊 ? System ? TObject ? GetInterface
頭文件:#include <systobj.h>
命名空間:System
函數(shù)原型:
bool __fastcall GetInterface(const GUID &IID, /* out */ void *Obj);
template <typename T>
bool __fastcall GetInterface(DelphiInterface<T> &smartIntf)
{
return GetInterface(__uuidof(T), reinterpret_cast<void*>(static_cast<T**>(&smartIntf)));
}
System::TObject::GetInterface 是 System::TObject 的成員函數(shù)壤圃,獲取指定的接口墨叛。GetInterface 相當于 Delphi 里面的 as 和 C++ 里面的 dynamic_cast漠趁,但是不會拋出異常甥绿,如果指定的接口不支持。
參數(shù):
Delphi 的 IID 允許用接口名稱翩活,編譯器會自動采用類型的 GUID。
C++ 可以用模板版本的 GetInterface舷嗡,相當于 dynamic_cast咬崔,GetInterface 在轉為不支持的類型的時候不會拋出異常垮斯,只是得到的指針為 NULL。
參數(shù) Obj 雖然是 void * 類型的抛寝,但是實質上是 void ** 類型的晶府,必須把 void ** 強制轉為 void * 作為 Obj 參數(shù)蛮位。
返回值:
true: 成功萄焦,通過 Obj 參數(shù)返回接口躲株;
false: 失敗缘回。
參考:
- System::TObject::GetInterfaceEntry
- System::TObject::GetInterfaceTable
- System::Classes::TComponent::QueryInterface
- System::Classes::TComponent::IsImplementorOf
- System::Classes::TComponent::ReferenceInterface
- System::Classes::TComponent::GetTypeInfo
- System::Classes::TComponent::GetTypeInfoCount
- System::Classes::TComponent::GetIDsOfNames
- System::Classes::TComponent::operator _di_IInterface
- System::Classes::TInterfacedPersistent::operator _di_IInterface
- System::TObject
- VCL 類繼承關系
C++ Builder 參考手冊 ? System ? TObject ? GetInterface