#include <iostream>
struct A {
unsigned int foo () const {
return 7;
}
};
struct B {
virtual unsigned int foo () const = 0;
};
void go (B * b) {
std::cout << b->foo();
}
struct C : public A, public B {
};
int main () {
go (new C());
}
Multiple inheritence quickie
Can someone please tell me how to make this compile?
The compiler complains that B::foo isn't implemented in C, but I say that it is. How should I best declare that C::B::foo is identical to C::A::foo?
It's your job to resolve ambiguity.
struct C : public A, public B { unsigned int foo () const { return A::foo (); }};
In this case C is an abstract class because it inherits from an abstract class (B) and does not provide an implementation for method foo.
Keep in mind that the function foo in A is NOT the same as in B... It would be if A inherited from B...
Keep in mind that the function foo in A is NOT the same as in B... It would be if A inherited from B...
If you want a sister class to provide an implementation to an interface, then you need to use virtual inheritance:
See the C++ FAQ lite for more info.
struct Interface { virtual unsigned int foo () const = 0;};struct Implementation : public virtual Interface { unsigned int foo () const { return 7; }};struct BaseClass : public virtual Interface {};struct DerivedClass : public BaseClass, public Implementation {};//DerivedClass implements the abstract function foo (which it inherited from BaseClass) by inheriting Implementation.
In my opinion you should avoid designs like this though as it might lead to the so called "diamond of death" (http://en.wikipedia.org/wiki/Diamond_problem)
Quote:Original post by vkalpias
In my opinion you should avoid designs like this though as it might lead to the so called "diamond of death" (http://en.wikipedia.org/wiki/Diamond_problem)
It's the programmer's version of "guns don't kill people", isn't it?
@Hodgman thanks for the example
Quote:Original post by vkalpias
In my opinion you should avoid designs like this though as it might lead to the so called "diamond of death" (http://en.wikipedia.org/wiki/Diamond_problem)
That's exactly what virtual inheritance is meant to solve.
Java effectively automates the solution to the problem by allowing extension of exactly one class (exactly rather than at most, because extending java.lang.Object is implicit otherwise), but implementation of multiple interfaces.
Quote:Original post by vkalpiasIn my example, "Interface" *is* at the top of a dreaded diamond ;)
In my opinion you should avoid designs like this though as it might lead to the so called "diamond of death" (http://en.wikipedia.org/wiki/Diamond_problem)
However, "Interface" is only ever inherited using "public virtual" (instead of just "public") so it is only inherited once (instead of twice) and the diamond problem is solved.
That said... this isn't a very common way of solving problems in C++, probably because most people are just plain afraid or ignorant about multiple-inheritance ;)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement