问题
ATL END_COM_MAP macro is defined as follows:
#define END_COM_MAP() \
__if_exists(_GetAttrEntries) {{NULL, (DWORD_PTR)_GetAttrEntries, _ChainAttr }, }\
{NULL, 0, 0}}; return _entries;} \
virtual ULONG STDMETHODCALLTYPE AddRef( void) throw() = 0; \
virtual ULONG STDMETHODCALLTYPE Release( void) throw() = 0; \
STDMETHOD(QueryInterface)(REFIID, void**) throw() = 0;
It is intended to be used within definition of classes inherited from COM interfaces, for example:
class ATL_NO_VTABLE CMyClass :
public CComCoClass<CMyClass, &MyClassGuid>,
public CComObjectRoot,
public IMyComInterface
{
public:
BEGIN_COM_MAP( CMyClass )
COM_INTERFACE_ENTRY( IMyComInterface)
END_COM_MAP()
};
This means that QueryInterface(), AddRef() and Release() are declared as pure virtual in this class. Since I don't define their implementation this class should be uncreatable. Yet ATL successfully instantiates it.
How does it work and why are those IUnknown member functions redeclared here?
回答1:
It's been a while since I used ATL but, IIRC, what ends up being instantiated is not CMyClass
, but CComObject<CMyClass>
.
CComObject
implements IUnknown
and inherits from its template parameter.
Edit: The "Fundamentals of ATL COM Objects" page on MSDN nicely illustrates what's going on.
来源:https://stackoverflow.com/questions/1729834/whats-the-purpose-of-iunknown-member-functions-in-end-com-map