views:

292

answers:

2

Is there a way to define a class Foo in C++ so that

  1. I can inherit from it
  2. I can't "diamond inherit" from it

I.e.

class Cat: public Foo{} // okay
class Dog: public Foo{} // okay
class Weird: public Cat, public Dog {} // I want this to throw a compiler error
+6  A: 

Cprogramming.com Tutorial: Solving the Diamond Problem in C++ with ...

http://www.cprogramming.com/tutorial/virtual_inheritance.html

Try This

For this type problem can be Avoid or solve by interface.

ratty
Amazing. I didn't realize there's "virtual" inheritance.
anon
The problem is that `virtual` inheritance is on hand of those who derive, you can't really do anything to change that... so just hope those who write derivative classes read your comments ;)
Matthieu M.
+1  A: 

Another source of information:

http://www.parashift.com/c++-faq-lite/multiple-inheritance.html

Actually the whole C++ faq little it is really worth reading if you are programming on C++.

fco.javier.sanz
And the associated book is good too.
markh44