• Advertisement
Sign in to follow this  

Multiple inheritence quickie

This topic is 3375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone please tell me how to make this compile?
#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());
}

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?

Share this post


Link to post
Share on other sites
Advertisement
It's your job to resolve ambiguity.


struct C : public A, public B {
unsigned int foo () const {
return A::foo ();
}
};


Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
If you want a sister class to provide an implementation to an interface, then you need to use virtual inheritance:
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.


See the C++ FAQ lite for more info.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)
In my example, "Interface" *is* at the top of a dreaded diamond ;)
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 ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement