Re: I wish c++ did interfaces better.
On Aug 7, 2:35 am, Stuart Golodetz
<sgolod...@NdOiSaPlA.pMiPpLeExA.ScEom> wrote:
Sorry to piggy-back on this thread, but it reminded me of a
related interfaces problem I had a while back actually (I
found a way to circumvent it, but it was a bit
unsatisfactory).
Suppose you're implementing an inheritance hierarchy of
Cartesian coordinate systems. You start off with interfaces:
ICoordSystem <-- IOrthogonalCoordSystem <-- IOrthonormalCoordSystem
You then want to implement each of these concretely:
ICoordSystem <-- CoordSystem
IOrthogonalCoordSystem <-- OrthogonalCoordSystem
IOrthonormalCoordSystem <-- OrthonormalCoordSystem
The only problem is, you want to reuse the logic in
CoordSystem etc., so you also have this:
CoordSystem <-- OrthogonalCoordSystem <-- OrthonormalCoordSystem
At this point, I started having fun with my compiler. It was a
while back now, so I can't quite remember what I tried any
more before giving up and doing it another way, but how should
I have done it please, out of interest?
This is a classical case; I've seen it both with interfaces (as
in your example), and with machine generated code instead of
your interfaces (the machine generated code provides a simple,
default implementation, which you can override). I call it a
"ladder hierarchy", although I don't think I've seen this term
elsewhere:
ICoordSystem <--- CoordSystem
^ ^
| |
| |
IOrthogonalCoordSystem <--- OrthogonalCoordSystem
^ ^
| |
| |
IOrthonormalCoordSystem <--- OrthonormalCoordSystem
(Except that in one case I encountered, it extended some six or
seven layers deep.)
Basically, anytime a class has more than one arrow leading into
it, inheritance must be virtual; the simplest solution is just
to make all heritage of the interface virtual:
class ICoordSystem { /* ... */ } ;
class IOrthogonalCoordSystem
: public virtual ICoordSystem { /* ... */ } ;
class IOrthonormalCoordSystem
: public virtual IOrthogonalCoordSystem { /* ... */ } ;
class CoordSystem
: public virtual ICoordSystem { /* ... */ } ;
class OrthogonalCoordSystem
: public virtual IOrthogonalCoordSystem
: private CoordSystem { /* ... */ } ;
class OrthonormalCoordSystem
: public virtual IOrthonormalCoordSystem
: private OrthogonalCoordSystem { /* ... */ } ;
(Note that the inheritance of the implementation is private.
This is usual as well, although not always necessary.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34