Our code-base has a lot of COM objects, so typically we'll have something like:
//this is the IDL-generated IGetTime interface header
#include "gettime.h"
//our COM class implementation of IGetTime
class CGetTime : public IGetTime
{
public:
CGetTime(CGetTimeCF &factory,...); //COM-specific constructor params
/* IUnknown */
STDMETHODIMP QueryInterface(REFIID riid, LPVOID FAR * ppv);
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
/* IGetTime */
STDMETHODIMP GetTheTime(); //
};
The way the constructor and a few other things are set up, this class is only easily usable through COM... you can hack a way to create the object but it tends to be flaky. I many cases though, the actual functionality of the class might be usable without COM... and this would also be more easily testable if you could do new CGetTime();
I wondered about having something like this as a pattern:
class CGetTimeBase
{
public:
STDMETHODIMP GetTheTime();
};
class CGetTime : public IGetTime, public CGetTimeBase
{
...
};
Is this a good approach, or is there a better way to have a 'plain C++' class providing the core functionality, and a COM class doing the COM stuff? To minimise amount of code to write I'd rather not introduce wrapper methods, I think, but have the actual implementation method in the non-COM class so it's automatically present in the COM class.
Thoughts? };