Possible Duplicate:
Why does C++ not let baseclasses implement a derived class' inherited interface?
#include <iostream>
class Interface
{
public:
virtual void yell(void) = 0;
};
class Implementation
{
public:
void yell(void)
{
std::cout << "hello world!" << std::endl;
}
};
class Test: private Implementation, public Interface
{
public:
using Implementation::yell;
};
int main (void)
{
Test t;
t.yell();
}
I want the Test class to be implemented in terms of Implementation, and I want to avoid the need to write the
void Test::yell(void) { Implementation::yell(); }
method. Why it is not possible to do it this way? Is there any other way in C++03?
usingonly brings a name into a scope.It doesn’t implement anything.
If you want Java-like get-implementation-by-inheritance, then you have to explicitly add the overhead associated with that, namely
virtualinheritance, like this:EDIT: a bit sneaky this feature, I had to edit to make the code compile with g++. Which didn’t automatically recognize that the implementation
yelland the interfaceyellwere one and the same. I’m not completely sure what the standard says about that!