views:

70

answers:

1

Ok, I'm using virtual functions, overloaded functions, and multiple inheritance. Of course this doesn't turn out well.

The scenario: Class base1 has a virtual function that needs to be specified by its child. Class derived derives from two parents base1 and base2, and should use base2's existing functionality to define base1's virtual function.

This is ok, but awkward of course. The motivation is I cannot change the class base2 and the existing interface already heavily invested in has base1 and base2 class functions of the same name. This is ok, nothing is implemented in base1, it should just redirect to base2.

My problem arises because base2 has several overloaded functions of the same name as the virtual function in question. All of the other overloaded versions become essentially hidden at compile time.

Here's a small demonstration code.

// this version does not compile, overloaded samenameFunc(int, int) cannot be found in the derived class.
#include <iostream>
using namespace std;

class base1 {
public:
    virtual void samenameFunc(int scratch) { cout << "from base1: " << scratch << std::endl; }
};

class base2 {
public:
    void samenameFunc(int scratch) { cout << "samenameFunc from base2: " << scratch << std::endl; }
    void samenameFunc(int scratch, int foo)  { cout << "samenameFunc(2 args) from base2: " << scratch << ", " << foo << std::endl; }
};

class derived : public base1, public base2 {
public:
    void samenameFunc(int scratch) { base2::samenameFunc(scratch); }// { cout << "from derived: " << scratch << std::endl; }
};

int main()
{
    derived d;

    d.samenameFunc(66);
    d.samenameFunc(77, 88);
    return 0;
}

And the compiler errors:

$ g++ inheritance_overload_tester.cc 
inheritance_overload_tester.cc: In function ‘int main()’:
inheritance_overload_tester.cc:26: error: no matching function for call to ‘derived::samenameFunc(int, int)’
inheritance_overload_tester.cc:18: note: candidates are: virtual void derived::samenameFunc(int)

If the overloaded samenameFunc(int, int) is renamed, I can compile and function as expected. But remember, I can't actually change base2.

// this version does compile
#include <iostream>
using namespace std;

class base1 {
public:
    virtual void samenameFunc(int scratch) { cout << "from base1: " << scratch << std::endl; }
};

class base2 {
public:
    void samenameFunc(int scratch) { cout << "samenameFunc from base2: " << scratch << std::endl; }
    void anotherFunc(int scratch, int foo)  { cout << "anotherFunc(2 args) from base2: " << scratch << ", " << foo << std::endl; }
};

class derived : public base1, public base2 {
public:
    void samenameFunc(int scratch) { base2::samenameFunc(scratch); }// { cout << "from derived: " << scratch << std::endl; }
};

int main()
{
    derived d;

    d.samenameFunc(66);
    d.anotherFunc(77, 88);
    return 0;
}

If this can't be fixed in the existing presentation, can someone suggest a different object inheritance model to solve my problem?

+1  A: 

In the definition of the derived class, you can add a using declaration to make the function from base2 visible:

using base2::samenameFunc;
James McNellis
Ah yes. This is working for me. Thank you thank you.
NoahR