So, having come from a background of Java as my first language, and being of the opinion that C# is a nearly-perfect OOPL, I am frequently annoyed with C++'s lack of support for interfaces.
I often will "solve" this by making use of multiple inheritance, but recently I had a situation arise that got me thinking....
Let's say I have these classes:
struct Interface { virtual char foo() const = 0; }; struct Base: public Interface { int z; bool blah() const; virtual char foo() const override; };And now I want to create a class Derived. If I make Derived inherit from Base, then it also inherits from Interface. But what if I want different access rules for the members inherited from Base and those inherited from Interface? Say I want Derived::blah() and Derived::z to have protected access, while Derived::foo() has public access.
Virtual inheritance, I think, would probably work for this... however, what if I must also be able to downcast data of type Interface* to Derived*? Virtual inheritance makes this type of casting impossible (except with reinterpret_cast, but that is probably the nastiest curse word in the C++ vocabulary that I know of)
Is it then acceptable to declare Derived like so?
struct Derived: protected Base, public Interface { ... };This means that each Derived object will have two Interface sub-objects, but is that really an issue, seeing as the Interface class consists only of function members, with no data?
Thanks!!
Edited by W-Unit, 25 August 2011 - 02:55 PM.