Obscure usages of C++ virtual inheritance -
the virtual inheritance in c++ useful way prevent diamond issue. however, can't seem make work in each , every case.
this going hard explain hope i'll manage. let's present problem:
a inherits b. , base class of class set called c , class set called d.
the problem c set of class have common features focusing around b.
what do, isn't possible, class e virtually inherits b, , inherited c. problem is:
in case, doesn't virtually inherits b. doesn't work.
if virtually inherit b, need use b's constructor in every classes of d.
conclusion: in every case there duplicated code. how may out of issue without duplicated code ?
okay saying have:
b + | v + +----+----+ v v c d
and of these non-virtual inheritance , have class e
virtualy inherits b
, c
inherits complete diagram be:
+-----------+b | + | | v v e + + +---+---+----+----+ v v c d
with exception b
virtualy inherited e
.
the solution simple should make a
virtually inherit b
, gathered question can not modify a
. next option make c
inherit a
or e
, have instance of other called delegation. alternative can provide common interface , allow polymorphism might want make interface (pure virtual class) has function in e
c
uses , make c
, e
derive interface. e
implement virtual functions interface , c implement function calls e
's implementation. alternatively (not recommended) can live diamond of death if not problematic you, determining tricky , don't recommend it's terrible idea.
Comments
Post a Comment