当前位置:网站首页>About queryinterface functions

About queryinterface functions

2022-06-25 17:35:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

One ,QueryInterface function Prototype :

HRESULT __stdcall QueryInterface(const IID&iid,void **ppv);

iid: Mark the interface required by the customer . yes ” An interface identifier “ structure (IID). ppv:QueryInterface Used to store the address of the requested interface .

Return value : Can return S_OK or E_NOINTERFACE Should use the SUCEEDED perhaps FAILED Macro validation successful .

Usage method :

If you know a direction IUnknown Pointer to interface pI, Just pass it an interface identifier

for example :

void
 Foo(IUnknown 
*
 pI)
QueryInterface Function related QueryInterface function 

{

QueryInterface function     IX * pIX = NULL;
QueryInterface function 
QueryInterface function     HRESULT hr = pI->QueryInterface(IID_IX,(void **)pIX);
QueryInterface function 
QueryInterface function     if(SUCCEEDED(hr))
QueryInterface Function related QueryInterface function     {

QueryInterface function       pIX->FX();
QueryInterface function      }
QueryInterface function }

QueryInterface function 
QueryInterface function 
//
QueryInterface The implementation of the .

QueryInterface function 

QueryInterface function HRESULT _stdcall  CA::QueryInterface(
const
    IID
&
iid,
void
 
**
ppv)
QueryInterface Function related QueryInterface function 

{

QueryInterface function     if(iid == IID_IUnknown)
QueryInterface Function related QueryInterface function     {

QueryInterface function       *ppv = static_cast<IX *>(this);
QueryInterface function     }
QueryInterface function     else  if(iid == IID_IX)
QueryInterface Function related QueryInterface function     {

QueryInterface function       *ppv = static_cast<IX *>(this);
QueryInterface function     }
QueryInterface function     else  if(iid == IID_IY)
QueryInterface Function related QueryInterface function     {

QueryInterface function       *ppv = static_cast<IY *>(this);
QueryInterface function     }
QueryInterface function     else  if(iid == IID_IUnknown)
QueryInterface Function related QueryInterface function     {

QueryInterface function       *ppv = NULL;
QueryInterface function       return E_NOINTERFACE;
QueryInterface function     }
QueryInterface function 
QueryInterface function     static_cast <IUnknown *> (* ppv)->AddRef();
QueryInterface function     retru S_OK;
QueryInterface function 
QueryInterface function }
 Multiple inheritance and type conversion .

 Usually converting a pointer of one type to another does not change its value . To support multiple inheritance , In some cases ,C++ You must change the value of the class pointer .
 Suppose a class is defined as follows :

class CA: public IX,public IY{…};

 because CA At the same time inherited IX,IY So you can use IX or IY The pointer can be used everywhere CA The pointer to .

 

QueryInterface function 
void
 foo(IX
*
 pIX);
QueryInterface function 
void
 bar(IY
*
 pIY);
QueryInterface function 
int
 main()
QueryInterface Function related QueryInterface function 

{

QueryInterface function  CA* pA = new CA;
QueryInterface function  foo(pA);
QueryInterface function  bar(pA);
QueryInterface function  delete pA;
QueryInterface function  return 0;
QueryInterface function }

QueryInterface function 

foo Need a legal IX Pointer to the virtual function table of ; bar Need a legal IY Pointer to the virtual function table of ; Of course IX and IY The contents of the virtual function table of are different . So... One IX vtbl Pass to bar when , This function does not work properly . So the compiler passes the same pointer to foo and bar It's impossible , It has to be right CA Modify the pointer of so that it points to an appropriate vtbl The pointer . The image below shows CA Object's memory structure .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/151157.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251711139436.html