Search code examples
c++visual-c++comatldcom

How do I set the interface on CComPtr in a Superclass?


I want to try and modify my code to use a superclass to handle creating CComPtr, but I'm not sure how to pass the class to the CComPtr to create, ie the part in

void CSuperClass::CreateSmartPointer(CString class, Interface interface)
{
   CLSID clsid;
   hr = CLSIDFromProgID(class, &clsid);
   CComPtr<interface> spInterface;
   hr = spInterface.CoCreateInstance(clsid, 0, CLSCTX_ALL);
}


void CSubClass::Init()
{

    CreateSmartPointer("MYServer.MyClass", xxx);
}

void CSubClass2::Init()
{

    CreateSmartPointer("MYServer2.MyClass2", xxx);
}

Solution

  • Depending on what you want to achieve, templates can do the job:

    template<class Interface> class CSuperClass { 
        // ...
        void CreateSmartPointer(CString class) {
            // ...
            CComPtr<Interface> spInterface;
            // ....