C++ virtual method: best way to use base implementation with an addition -
say have following classes:
class airplane { virtual bool fly(uint64_t destinationid) { //do airplane flown. } /* * more function , data members. */ } class somemodel: public airplane { virtual bool fly(uint64_t destinationid); { //do somemodel should before gets flying. //do airplane::fly does. } } my question how implement somemodel::fly. 1 simple way follows:
virtual bool somemodel::fly(uint64_t destinationid) { //do somemodel should before gets flying. airplane::fly(destinationid); } is there nicer way of doing it? or there reason choosing way. know general question it's first time have implement such method want make sure i'm not missing anything.
edit
i find worth emphasize airplane not general or abstract class, many airplane in company airplanes , appear such without inhritance, there 1 specific model though has specific behavior.
this depends on trying achieve. example valid , 1 solution 1 type of problem (where setup or other variations required on).
another variant on theme use virtual setup, , common "fly" method.
so:
class airplane { bool fly(uint64_t destinationid) { setupforflight(); // actual flying stuff ... ... } virtual void setupforflight() { // nothing standard airplane } } class boeing747: public airplane { ... void setupforflight() { ... stuff needs set here. } ... } there benefits both of these methods, , depend on modelling better.
of course, have afterlanding type function @ end of fly well.
just out of curiousity, there many destinations need 64-bit value them - i've never considered it, curious.
edit: think i'm describing "template method pattern". i'm not great names these things, know how it's working....
Comments
Post a Comment