c++ - How to have a derived class use the base implemention to satisfy an interface -
i have following 2 interfaces, not part of inheritance hierarchy. have 2 concrete classes, 1 derives other.
class icar { public: virtual void drive() = 0; }; class ifastcar { public: virtual void drive() = 0; virtual void drivefast() = 0; }; class car : public virtual icar { public: void drive() {}; }; class fastcar : public car, public virtual ifastcar { public: void drivefast() {}; }; int main() { fastcar fc; fc.drive(); fc.drivefast(); return 0; }
when compile following error:
error: cannot declare variable `fc' of type `fastcar' error: because following virtual functions abstract: error: virtual void ifastcar::drive()
my program work if write function in fastcar delegate drive() base class
void drive() { car::drive(); }
is possible have fastcar compile without writing methods delegate base class?
note: icar , ifastcar created 2 different teams , in 2 different projects. teams have agreed on common method signatures operations shared. have used inheritance in implementation classes attempt reuse parts of implementation same.
the problem lies in fact there's multiple inheritance taking place here ... though fastcar
derived car
, version of drive()
in base-class car
overrides icar::drive()
, not ifastcar::drive()
. thus, since deriving fastcar
ifastcar
, need to, @ point, define function in fastcar
overrides pure virtual abstract method ifastcar::drive()
... inherited car::drive()
not automatically override ifastcare::drive()
since not inherit class. ifastcar::drive()
, icar::drive()
2 different pure abstract virtual functions need overridden separately. if want make interface ifastcar::drive()
call inherited car::drive()
function, need delegate inherited function you've done within version of fastcar::drive()
calls base-class' version of drive()
.
Comments
Post a Comment