Re: IUnknown



Thanks Giovanni,


Your reply is comprehensive and my question is answered.


regards,
George

"Giovanni Dicanio" wrote:


"George" <George@xxxxxxxxxxxxxxxxxxxxxxxxx> ha scritto nel messaggio
news:10443EBF-25C1-4DED-A5D1-4226951A4F2F@xxxxxxxxxxxxxxxx
you can see QueryInterface has two forms
of implementation, one is pure virtual function, the other is not. From
traditional COM book, QueryInterface should be a pure virtual function.
Correct me if I am wrong.

George: all COM *interfaces* are a set of pure virtual functions.
Then, when you implement a COM interface, you inherit from that interface,
and you write (or reuse) a body for the interface pure virtual functions,
which become class methods with a body, in C++.

Any ideas?

IUnknown
{
public:
BEGIN_INTERFACE
virtual HRESULT STDMETHODCALLTYPE QueryInterface(
/* [in] */ REFIID riid,
/* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject) =
0;


This is a pure virtual function, in fact it has the form "virtual ....
f(...) = 0 ".

"virtual" --> virtual function (or better virtual method)
"= 0" --> pure


template<class Q>
HRESULT STDMETHODCALLTYPE QueryInterface(Q** pp)
{
return QueryInterface(__uuidof(Q), (void **)pp);
}

This seems a C++ template "helper" *implementation* of QueryInterface, that
allows the caller to use an interface (Q), instead of the interaface ID.
In fact, this implementation gets the Q interface ID using __uuidof.
The "real" QueryInterface specification is the first you read.
This second one is just an helper template implementation for QueryInterface
virtual method, to allow programmers to write something like this

IMyInterface * pMyInterface;

...QueryInterface( &pMyInterface )

instead of the more "verbose":

.... QueryInterface( IID_IMyInterface, reinterpret_cast<void **>(
&pMyInterface ) );

HTH,
Giovanni




.



Relevant Pages

  • Re: urgent about STDMETHOD
    ... the class is an interface and the derived class ... Technically this is called a pure virtual function. ... base class-- to give some default functionality that the overriding function ...
    (microsoft.public.vc.mfc)
  • Re: IUnknown
    ... QueryInterface should be a pure virtual function. ... Then, when you implement a COM interface, you inherit from that interface, ... virtual HRESULT STDMETHODCALLTYPE QueryInterface( ...
    (microsoft.public.vc.language)
  • Re: Class Inheritance vs Interface Inheritance
    ... If a pure virtual function has an implementation, it usually serves as a default implementation, with the purpose of being called from an inherited class. ... I think it sort of undermines the purity of the interface concept. ... When we talk about interfaces as design patterns, we assume that the class has no data members. ...
    (microsoft.public.vc.language)
  • Re: non-pure virtual function in C++ interface
    ... can provide non-pure virtual function. ... Previously I think in interface, ... pure virtual function is allowed. ... Tim Roberts, timr@xxxxxxxxx ...
    (microsoft.public.vc.language)
  • Re: IUnknown
    ... Here is the related part of code, you can see QueryInterface has two forms ... QueryInterface should be a pure virtual function. ... virtual ULONG STDMETHODCALLTYPE Release(void) = 0; ...
    (microsoft.public.vc.language)