Re: Why doesn't this multiple virtual inheritance code compile?
On 03.01.2012 05:25, Chris Stankevitz wrote:
I do not want all Shapes to be observerImps. I want all shapes to be
observers. I suspect what I want is not possible with c++.
What you are going to do is to have a helper class OberserImp that
implements the observer interface independently of the outer class. And
you want to be able to use this helper class down in the class tree not
only at the level of the interface reference in the class Shape.
I know two solution for this in C++.
1. The Java way.
The equivalent of interfaces in Java and .NET are virtual abstract base
classes in C++. If you want the Java behavior you always need to derive
virtual from all interface like C++ classes.
That was your approach except for the missing virtual at class Shape.
2. An template implememtation helper.
template <typename BASE>
class ObserverImp : BASE
{
void Notify() {}
};
class Square : public ObserverImp<Shape>
{
};
#1 has the disadvantage that it always requires another level of
indirection to access interface members at run time, even if this is not
required. And in real life this is required quite rarely.
The performance impact of this indirection is usually no big deal, but
the reduced possibilities for optimization and especially inlining could
be relevant.
Marcel