Inherit interfaces that share a method name

Two base classes have the same function name. I want to inherit both of them, and use each method differently. How can I do this with a separate declaration and definition (instead of the definition in the class definition)?

#include <cstdio> class Interface1{ public: virtual void Name() = 0; }; class Interface2 { public: virtual void Name() = 0; }; class RealClass: public Interface1, public Interface2 { public: virtual void Interface1::Name() { printf("Interface1 OK?\n"); } virtual void Interface2::Name() { printf("Interface2 OK?\n"); } }; int main() { Interface1 *p = new RealClass(); p->Name(); Interface2 *q = reinterpret_cast<RealClass*>(p); q->Name(); } 

I was unable to move the definition to VC8. I found the Microsoft Specific Keyword __interface keyword, having successfully completed this work, the code below:

 #include <cstdio> __interface Interface1{ virtual void Name() = 0; }; __interface Interface2 { virtual void Name() = 0; }; class RealClass: public Interface1, public Interface2 { public: virtual void Interface1::Name(); virtual void Interface2::Name(); }; void RealClass::Interface1::Name() { printf("Interface1 OK?\n"); } void RealClass::Interface2::Name() { printf("Interface2 OK?\n"); } int main() { Interface1 *p = new RealClass(); p->Name(); Interface2 *q = reinterpret_cast<RealClass*>(p); q->Name(); } 

but is there any other way to make this something more general that will work on other compilers?

+41
c ++ gcc multiple-inheritance visual-c ++
Jan 05 '10 at 8:35
source share
5 answers

This problem does not occur very often. The solution I am familiar with was developed by Doug McIlroy and appeared in the books of Bjarn Straustrup (presented in the section "Design and Evolution of the C ++ Section" 12.8 and "C ++ Programming Language", section 25.6). According to the discussion in Design and Evolution, it was proposed to elegantly handle this particular case, but it was rejected because “such name clashes are unlikely to become common enough to guarantee a separate language feature” and “work for beginners is unlikely to become everyday. "

You need to not only call Name() with pointers to the base classes, you need to specify which Name() you want to use with the derived class. The solution adds some indirectness:

 class Interface1{ public: virtual void Name() = 0; }; class Interface2{ public: virtual void Name() = 0; }; class Interface1_helper : public Interface1{ public: virtual void I1_Name() = 0; void Name() override { I1_Name(); } }; class Interface2_helper : public Interface2{ public: virtual void I2_Name() = 0; void Name() override { I2_Name(); } }; class RealClass: public Interface1_helper, public Interface2_helper{ public: void I1_Name() override { printf("Interface1 OK?\n"); } void I2_Name() override { printf("Interface2 OK?\n"); } }; int main() { RealClass rc; Interface1* i1 = &rc; Interface2* i2 = &rc; i1->Name(); i2->Name(); rc.I1_Name(); rc.I2_Name(); } 

Not very, but the solution was not needed often.

+56
Jan 05 '10 at 9:44
source share

In the past, I had to do something similar, although in my case I needed to inherit one interface twice and be able to distinguish between calls made on each of them, I used a template pad to help me ...

Something like that:

 template<class id> class InterfaceHelper : public MyInterface { public : virtual void Name() { Name(id); } virtual void Name( const size_t id) = 0; } 

Then you exit InterfaceHelper twice, not MyInterface twice, and you specify a different id for each base class. You can then pass the two interfaces independently by executing the correct InterfaceHelper .

You can do something a little more complicated;

 class InterfaceHelperBase { public : virtual void Name( const size_t id) = 0; } class InterfaceHelper1 : public MyInterface, protected InterfaceHelperBase { public : using InterfaceHelperBase::Name; virtual void Name() { Name(1); } } class InterfaceHelper2 : public MyInterface, protected InterfaceHelperBase { public : using InterfaceHelperBase::Name; virtual void Name() { Name(2); } } class MyClass : public InterfaceHelper1, public InterfaceHelper2 { public : virtual void Name( const size_t id) { if (id == 1) { printf("Interface 1 OK?"); } else if (id == 2) { printf("Interface 2 OK?"); } } } 

Note that there was no compiler above ...

+5
Jan 05 '10 at 9:42 on
source share

You cannot redefine them separately; you must redefine both points:

 struct Interface1 { virtual void Name() = 0; }; struct Interface2 { virtual void Name() = 0; }; struct RealClass : Interface1, Interface2 { virtual void Name(); }; // and move it out of the class definition just like any other method: void RealClass::Name() { printf("Interface1 OK?\n"); printf("Interface2 OK?\n"); } 

You can simulate an individual override with intermediate base classes:

 struct RealClass1 : Interface1 { virtual void Name() { printf("Interface1 OK?\n"); } }; struct RealClass2 : Interface2 { virtual void Name() { printf("Interface2 OK?\n"); } }; struct RealClass : RealClass1, RealClass2 { virtual void Name() { // you must still decide what to do here, which is likely calling both: RealClass1::Name(); RealClass2::Name(); // or doing something else entirely // but note: this is the function which will be called in all cases // of *virtual dispatch* (for instances of this class), as it is the // final overrider, the above separate definition is merely // code-organization convenience } }; 

Also, you are using reinterpret_cast incorrectly, you should have:

 int main() { RealClass rc; // no need for dynamic allocation in this example Interface1& one = rc; one.Name(); Interface2& two = dynamic_cast<Interface2&>(one); two.Name(); return 0; } 

And here rewrite CRTP , which may be what you want (or not):

 template<class Derived> struct RealClass1 : Interface1 { #define self (*static_cast<Derived*>(this)) virtual void Name() { printf("Interface1 for %s\n", self.name.c_str()); } #undef self }; template<class Derived> struct RealClass2 : Interface2 { #define self (*static_cast<Derived*>(this)) virtual void Name() { printf("Interface2 for %s\n", self.name.c_str()); } #undef self }; struct RealClass : RealClass1<RealClass>, RealClass2<RealClass> { std::string name; RealClass() : name("real code would have members you need to access") {} }; 

But note that here you cannot call Name on RealClass now (with virtual sending, for example rc.Name() ), you must first select the base. Macro photography is an easy way to clear CRTP broadcasts (usually access to membership is much more common in the CRTP database), but it can be improved . There's a short discussion of virtual sending in one of my other answers , but it is certainly better if anyone has a link.

+3
Jan 05
source share
 class BaseX { public: virtual void fun() { cout << "BaseX::fun\n"; } }; class BaseY { public: virtual void fun() { cout << "BaseY::fun\n"; } }; class DerivedX : protected BaseX { public: virtual void funX() { BaseX::fun(); } }; class DerivedY : protected BaseY { public: virtual void funY() { BaseY::fun(); } }; class DerivedXY : public DerivedX, public DerivedY { }; 
+1
Jan 6 '10 at 9:17
source share

There are two other related questions that ask almost (but not completely) the same thing:

Choosing common methods from inherited names . If you want rc.name () to call ic1-> name () or ic2-> name ().

Overriding common method names from (template) base classes . This has simpler syntax and less code than your decision, but does not allow access to functions from a derived class. Moreover, if you do not need to call name_i1 () from rc, you do not need to use things like InterfaceHelper.

0
Jan 6 '10 at 1:20
source share



All Articles