Cook Computing

ATL Object Wizard Generated Classes

May 28, 2002 Written by Charles Cook

I recently noticed something about ATL COM AppWizard generated classes which puzzled me (after several years of using ATL I should have noticed it before now). The generated code looks like this:


class ATL_NO_VTABLE CTstClass : 
  public CComObjectRootEx<CComSingleThreadModel>,
  public CComCoClass<CTstClass, &amp;CLSID_TstClass>,
  public ITstClass
{
public:
DECLARE_REGISTRY_RESOURCEID(IDR_TSTCLASS)
BEGIN_COM_MAP(CTstClass)
  COM_INTERFACE_ENTRY(ITstClass)
END_COM_MAP()
public:
  STDMETHOD(Foo)();
};

The issue that puzzled me was why do the interface method map and the interface methods have public visibility? The map macros need to be no more visible than protected for the case where map chaining from a base class is used, and I can't see why the interface methods shouldn't be defined as private because it wouldn't make sense to derive another COM class with the same CLSID as the base class. So, maybe the class should look like this:


class ATL_NO_VTABLE CTstClass : 
  public CComObjectRootEx<CComSingleThreadModel>,
  public CComCoClass<CTstClass, &CLSID_TstClass>,
  public ITstClass
{
public:
DECLARE_REGISTRY_RESOURCEID(IDR_TSTCLASS)
protected:
BEGIN_COM_MAP(CTstClass)
  COM_INTERFACE_ENTRY(ITstClass)
END_COM_MAP()
private:
  STDMETHOD(Foo)();
};

Defining the interface methods as public does allow the methods to be accessed from an instance of CTstClass but I think the default should be to prevent this and force the class to be used via the abstract interfaces. Am I missing something?