Inherit interfaces which share a method name

There are two base classes have same function name. I want to inherit both of them, and over ride each method differently. How can I do that with separate declaration and definition (instead of defining 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 failed to move the definition out in VC8. I found the Microsoft Specific Keyword __interface can do this job successfully, 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 another way to do this something more general that will work in other compilers?

How to implement virtual functions with the same name in multiple inheritance [duplicate]

This question already has an answer here: Inherit interfaces which share a method name 5 answers Have code as below // A has a virtual function F(). class A { public: virtual void F() {}; }; //

Inheritance from multiple interfaces with the same method name

If we have a class that inherits from multiple interfaces, and the interfaces have methods with the same name, how can we implement these methods in my class? How can we specify which method of which

Distinct implementations for pure virtual functions with same name [duplicate]

Possible Duplicate: Inherit interfaces which share a method name I have two base classes I1 and I2 with pure virtual functions void R() = 0;. I want the derived class IImpl to inherit from I1 and I2

Why does Java allow only a single method X when implementing 2 interfaces that both declare X but which differ in their throws clauses?

This is closely related to, but is not the same question as: Method name collision in interface implementation – Java When there are two Java interfaces that share a method with almost the same signat

Resolving ambiguity in an interface which extends two other interfaces which have a method with the same name

if I have interfaces: public interface ANewThing { IKey Key { get; } } public interface AnOldThing { object Key{ get; } } public interface ACompositeThing : ANewThing , AnOldThing { } and I write thi

Do interfaces inherit from Object class in java

Do interfaces inherit from Object class in Java? If no then how we are able to call the method of object class on interface instance public class Test { public static void main(String[] args) { Employ

Do subclasses inherit interfaces?

Quick question, I’m learning about interfaces and inheritance. This is not actual code, just an example. Let’s say I have the abstract class Animal. There’s some inheritance with groups like horses, a

Multiple interfaces with same method names [duplicate]

I have a class which inherits from two different interfaces. Both interfaces declare a method with the same name. How can I provide a different implementation for each interface ? In C#, the answer is

A class implements two interfaces. Which interface does a method belong to? [duplicate]

This question already has an answer here: Implementing two interfaces in a class with same method. Which interface method is overridden? 7 answers Implementing multiple interfaces having same me

Implementing two interfaces in a class with same method. Which interface method is overridden?

Two interfaces with same method names and signatures. But implemented by a single class then how the compiler will identify the which method is for which interface? Ex: interface A{ int f(); } interfa

Answers

You cannot override them separately, you must override both at once:

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 individual overriding 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
  }
};

Additionally, you’re 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’s a rewrite with CRTP that might 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 a RealClass now (with virtual dispatch, e.g. rc.Name()), you must first select a base. The self macro is an easy way to clean up CRTP casts (usually member access is much more common in the CRTP base), but it can be improved. There’s a brief discussion of virtual dispatch in one of my other answers, but surely a better one around if someone has a link.

I’ve had to do something like this in the past, though in my case I needed to inherit from one interface twice and be able to differentiate between calls made on each of them, I used a template shim to help me…

Something like this:

template<class id>
class InterfaceHelper : public MyInterface
{
    public : 

       virtual void Name() 
       {
          Name(id);
       }

       virtual void Name(
          const size_t id) = 0;  
}

You then derive from InterfaceHelper twice rather than from MyInterface twice and you specify a different id for each base class. You can then hand out two interfaces independently by casting to the correct InterfaceHelper.

You could do something slightly more complex;

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 the above hasn’t seen a compiler…

This problem doesn’t come up very often. The solution I’m familiar with was designed by Doug McIlroy and appears in Bjarne Stroustrup’s books (presented in both Design & Evolution of C++ section 12.8 and The C++ Programming Language section 25.6). According to the discussion in Design & Evolution, there was a proposal to handle this specific case elegantly, but it was rejected because “such name clashes were unlikely to become common enough to warrant a separate language feature,” and “not likely to become everyday work for novices.”

Not only do you need to call Name() through pointers to base classes, you need a way to say which Name() you want when operating on the derived class. The solution adds some indirection:

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()
    {
        I1_Name();
    }
};

class Interface2_helper : public Interface2{
public:
    virtual void I2_Name() = 0;
    void Name()
    {
        I2_Name();
    }
};

class RealClass: public Interface1_helper, public Interface2_helper{
public:
    void I1_Name()
    {
        printf("Interface1 OK?/n");
    }
    void I2_Name()
    {
        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 pretty, but the decision was it’s not needed often.

There are two other related questions asking nearly (but not completely) identical things:

Picking from inherited shared method names. If you want to have rc.name() call ic1->name() or ic2->name().

Overriding shared method names from (templated) base classes. This has simpler syntax and less code that your accepted solution, but does not allow for access to the functions from the derived class. More or less, unless you need to be able to call name_i1() from an rc, you don’t need to use things like InterfaceHelper.

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
{

};