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?
using
only 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 virtual
inheritance, like this:
#include <iostream>
class Interface
{
public:
virtual void yell() = 0;
};
class Implementation
: public virtual Interface
{
public:
void yell()
{
std::cout << "hello world!" << std::endl;
}
};
class Test: private Implementation, public virtual Interface
{
public:
using Implementation::yell;
};
int main ()
{
Test t;
t.yell();
}
yell
and the interface yell
were one and the same. I'm not completely sure what the standard says about that!