Deriving a class from more than one interface
-
How do you derive a class from more than one interface where two or more of the interfaces have methods with the same name, parameters and return type? class CSomeClass : public IInterfaceA, IInterfaceB { public: CSomeClass(); // IUnknown HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID FAR* ppvObj); STDMETHODIMP_(ULONG) AddRef(); STDMETHODIMP_(ULONG) Release(); // IIntefaceA HRESULT STDMETHODCALLTYPE DoStuff(VOID); // InterfaceB HRESULT STDMETHODCALLTYPE DoStuff(VOID); <-- Compiler generates an error }; error C2535: 'HRESULT CSomeClass::DoStuff()' : member function already defined or declared If its as simple as only having one copy of the function then how do you know which interface the function is serving? QueryInterface() handles this by passing in an IID but the functions I'm dealing with have no such parameters. Systems AXIS Ltd - Software for Business ...
-
How do you derive a class from more than one interface where two or more of the interfaces have methods with the same name, parameters and return type? class CSomeClass : public IInterfaceA, IInterfaceB { public: CSomeClass(); // IUnknown HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID FAR* ppvObj); STDMETHODIMP_(ULONG) AddRef(); STDMETHODIMP_(ULONG) Release(); // IIntefaceA HRESULT STDMETHODCALLTYPE DoStuff(VOID); // InterfaceB HRESULT STDMETHODCALLTYPE DoStuff(VOID); <-- Compiler generates an error }; error C2535: 'HRESULT CSomeClass::DoStuff()' : member function already defined or declared If its as simple as only having one copy of the function then how do you know which interface the function is serving? QueryInterface() handles this by passing in an IID but the functions I'm dealing with have no such parameters. Systems AXIS Ltd - Software for Business ...
One way around this problem would be to build intermediate C++ classes that derive from a single interface and impliments the clashing method by making a pure virtual call on a nonclashing name. For example: struct IXInterfaceA : public IInterfaceA { //Add new non clashing method as a pure virtual virtual HRESULT STDMETHODCALLTYPE DoStuffOnA(void) = 0; //impliment the clashing method STDMETHODIMP DoStuff(void) { return DoStuffOnA(); } } If you do the same for your other interface IInterfaceB Your CSomeClass would look like this. class CSomeClass : public IXInterfaceA, public IXInterfaceB { public: CSomeClass(); // IUnknown HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID FAR* ppvObj); STDMETHODIMP_(ULONG) AddRef(); STDMETHODIMP_(ULONG) Release(); // IIntefaceA HRESULT STDMETHODCALLTYPE DoStuffOnA(VOID); // InterfaceB HRESULT STDMETHODCALLTYPE DoStuffOnB(VOID); }; Hope this help, it helped me. Regards Andy