65

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?

Gohan
  • 2,422
  • 2
  • 26
  • 45
  • Why do you want this? It seems you're defeating the purpose of virtual functions; you're simply replicating non-virtual semantics. Do you have a reference for `__interface`? I looked it up and it doesn't appear to do anything essential. – Potatoswatter Jan 05 '10 at 09:15
  • 2
    Potatoswatter - the reason that you might need to do this is that you might have a class that brings together two different concepts by inheriting from two interfaces. The users of the class likely use it through the interfaces and the users of the two different interfaces don't know or care about each other. The class brings these together and needs to differentiate between calls on one interface and calls on the other. In no way are you defeating the purpose of virtual functions. Gohan - I'd be surprised if __inteface made a difference here. – Len Holgate Jan 05 '10 at 10:51
  • @Len It's different, If you want to move the implement part out directly, the __interface keyword will make it work, and using the standard class or struct only contain pure virtual functions will got error from compilers. – Gohan Jan 06 '10 at 14:35
  • 1
    @Potatoswatter "_you're simply replicating non-virtual semantics._" how exactly is that "replicating non-virtual semantics"? – curiousguy Nov 01 '11 at 00:52
  • So, is `Interface1::Name` an **un**qualified name? – curiousguy Nov 01 '11 at 04:15
  • An example where I find this pattern useful is for serialization. I have a hidden class that knows how to implement 2 interfaces, A & B. But when a user of A gets a reference to an object of that class and try to serialize it, they expect to serialize it as A, not as A &B. I rewrote Max's answer in a gist , renaming the functions to use "serialize". Perhaps seen like that makes more sense: https://gist.github.com/endavid/5fe5f65e80564d177e551769ac053d6b – endavid Oct 08 '19 at 08:49

5 Answers5

82

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() 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 pretty, but the decision was it's not needed often.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
  • 2
    You think and type very quickly ;v) . +1 – Potatoswatter Jan 05 '10 at 10:07
  • 3
    It's marvelous you provided the sources of the proposal, not quite sure though why didn't you include the contents of the proposal itself in the actual answer, as an "interesting fact" at least, however briefly. In order to fill myself in I had to look for the book online. Quite an interesting read. For future readers: the rejected proposal introduced the following syntax to "rename" a method of an implemented interface: `virtual void Name1() = Interface1::Name;` Cheers – Tomalla May 20 '17 at 12:58
  • @Tomalla Would be a nifty addition for a future C++ standard. :) – Harsh May 18 '20 at 12:28
9

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.

Community
  • 1
  • 1
  • 1
    Your intermediate base classes don't appear to do anything. You can still call `Interface1::Name` just the same as `RealClass1::Name`. – Potatoswatter Jan 05 '10 at 09:07
  • … I mean, you could, if it were implemented as for RealClass1. – Potatoswatter Jan 05 '10 at 09:10
  • You cannot call `rc.Interface1::Name()`, that is a pure virtual function without implementation. –  Jan 05 '10 at 09:18
  • Also note all this code is standard C++ and should work with any C++ compiler, though I actually only tested the CRTP case and merely proved the rest correct. –  Jan 05 '10 at 09:32
  • Covered that in my second comment ;v) . If he can add the intermediate class, he can probably add the implementation to the class he already has. Anyway, it just defeats the vtable lookup. – Potatoswatter Jan 05 '10 at 10:00
  • Not what I want, but the virtual dispatch part is great, thank you – Gohan Jan 05 '10 at 10:03
  • Potatoswatter: I don't understand what you're saying. I'm assuming he can't change the interface base, so "if it were implemented as for RealClass1" isn't possible. The intermediate class (which other answers use and Gohan said he found helpful) allows you to have separate final overriders for Interface1 and Interface2 (no other way to achieve that), and it allows an alternate way to specify base (`rc.RealClass1::Name()`, but with a better name, vs `((Interface1&)rc).Name()`). –  Jan 05 '10 at 10:38
  • Gohan: Sorry I couldn't present it better, but I'm glad some of it was helpful. –  Jan 05 '10 at 10:39
  • Overriding both at once is often sufficient. – John Z. Li May 29 '19 at 02:00
6

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...

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
  • No need to use a compiler to see that `Name(2);` cannot be a **recursive call** as this function `Name` takes no argument! – curiousguy Nov 01 '11 at 04:10
  • It's not intended to be a recursive call. The real implementations of Name() call a version which includes an Id so that you can tell which version of Name() was called in the base object. You then pass out the helper objects to whoever happens to need the actual interface. – Len Holgate Nov 01 '11 at 07:15
  • But as written it can only be a recursive call. You could to use the scope resolution operator to call another `Name` function, for example. – curiousguy Nov 01 '11 at 13:05
3
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
{

};
Jagannath
  • 3,995
  • 26
  • 30
2

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.

Community
  • 1
  • 1
Narfanator
  • 5,595
  • 3
  • 39
  • 71