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?

6
  • 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. Commented Jan 5, 2010 at 9: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. Commented Jan 5, 2010 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
    Commented Jan 6, 2010 at 14:35
  • 1
    @Potatoswatter "you're simply replicating non-virtual semantics." how exactly is that "replicating non-virtual semantics"?
    – curiousguy
    Commented Nov 1, 2011 at 0:52
  • So, is Interface1::Name an unqualified name?
    – curiousguy
    Commented Nov 1, 2011 at 4:15

5 Answers 5

83

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.

3
  • 2
    You think and type very quickly ;v) . +1 Commented Jan 5, 2010 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
    Commented May 20, 2017 at 12:58
  • @Tomalla Would be a nifty addition for a future C++ standard. :)
    – Harsh
    Commented May 18, 2020 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.

9
  • 1
    Your intermediate base classes don't appear to do anything. You can still call Interface1::Name just the same as RealClass1::Name. Commented Jan 5, 2010 at 9:07
  • … I mean, you could, if it were implemented as for RealClass1. Commented Jan 5, 2010 at 9:10
  • You cannot call rc.Interface1::Name(), that is a pure virtual function without implementation.
    – Roger Pate
    Commented Jan 5, 2010 at 9: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.
    – Roger Pate
    Commented Jan 5, 2010 at 9: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. Commented Jan 5, 2010 at 10: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...

3
  • No need to use a compiler to see that Name(2); cannot be a recursive call as this function Name takes no argument!
    – curiousguy
    Commented Nov 1, 2011 at 4: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. Commented Nov 1, 2011 at 7: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
    Commented Nov 1, 2011 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
{

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.