Topic: Covariant overriding [was Parallel inheritance hierarchies]
Author: Millour Christian <chris161@speedy.grolier.fr>
Date: 1996/09/06 Raw View
Dirk Bonne <h0444xiv@rz.hu-berlin.de> wrote:
>Bob Glickstein wrote:
>>
>> In the design for my application, there is a number of related class
>> hierarchies, such that if an instance of class B1 contains a member
>> belonging to class B2, then an instance of class D1 (derived from B1)
>> wants that member to belong to class D2 (derived from B2). What's the
>> best, most type-safe way to express this relationship? Thanks in
>> advance.
>
>You could use a virtual fuunction in B1:
>
>class B1 {
> ...
> virtual B2* getB2();
>};
>
>class D1 {
> ...
> virtual D2* getB2(); // changing return types.
>};
>
>So D1 could could use getB2 knowing it returns a D2.
>
>How to store the pointer to B2 is another problem, and depends on your
>application. For example, you could use a dynamic_cast (will not violate
>open/closed principle),.
>
>Or you could implement getB2 only in the leaf classes (non-leaf classes
>should be abstract anyway). That way you're completly typesafe.
>
>Dirk
>
An annoying problem with the covariant overriding described above is that
D2 must be fully declared before declaring D1 (the compiler has to know
that B1 is an accessible base of D1). This introduces extraneous
dependencies. Also it is impossible to override symmetrically :
class BR;
class BL { virtual BR * GetR(); }
class BR { virtual BL * GetL(); }
class DR;
class DL : public BL { DR * GetR(); } // oops ! won't compile
class DR : public BR { DL * GetL(); }
both problems might be solved with an extension of forward declaration:
class DR : public BR;
whose only purpose would be to make it clear that BR, not necessarily
fully declared at this point, is an accessible base of DR. This would keep
dependencies to a strict minimum (for clients of DR or DL), as in
--- BL.hh
class BR;
class BL { virtual BR * GetR(); }
--- BR.hh
class BL;
class BR { virtual BL * GetL(); }
--- DL.hh
#include "BL.hh"
class DR : public BR;
class DL : public BL { DR * GetR(); }
--- DR.hh
#include "BR.hh"
class DL : public BL;
class DR : public BR { DL * GetL(); }
--- DL.cc
#include "DL.hh"
#include "DR.hh" // needed to implement DL::GetR()
DR * DL::GetR() { ... }
I keep running into these issues when declaring abstract (public interface)
and concrete (secret implemetation) hierarchies. The extension of
forward declaration capabilities would greatly simplify the work.
I believe the issue was already raised in the past but cannot find anything
in my archives.
Comments, please ?
-- chris
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]