c++ - Design test with templates and inheritance -
i have question regarding design in c++. see in code below there design problem. want able have testclass inherits 0 or more classes derived modebase (modeone , modetwo in example). if testclass inherits modeone, have ability utilize methodeone(), , requirement testclass implement methodone() want.
class modebase { //--methods-------------------------------------------------------------------- public: virtual ~modebase() = default; }; class modeone : private modebase { //--methods-------------------------------------------------------------------- public: virtual ~modeone() = default; virtual void methodone() {} }; class modetwo : private modebase { //--methods-------------------------------------------------------------------- public: virtual ~modetwo() = default; virtual void methodtwo() {} }; class testbase { //--methods-------------------------------------------------------------------- public: testbase() : currentmode_( nullptr ) {} virtual ~testbase() = default; template <class mode, class t> void changemode() { if( std::is_base_of<mode, t>::value ) { // class inherit mode create sure current mode // has changed currentmode_ = std::make_shared<mode>(); } else { // class not inherit mode don't } } template <class mode> bool currentmode() { if( std::dynamic_pointer_cast<mode>(currentmode_) != nullptr ) { homecoming true; } homecoming false; } //--data members--------------------------------------------------------------- private: std::shared_ptr<modebase> currentmode_; }; class testone : public testbase , private modeone , private modetwo { //--methods-------------------------------------------------------------------- ~testone() = default; void heartbeattick() { if( currentmode<modeone>() ) { methodone(); } else if( currentmode<modetwo>() ) { methodtwo(); } } virtual void methodone() {} virtual void methodtwo() {} }; class somemanager { ~somemanager() = default; void changeallmode() { for( auto = vector_.begin(); != vector_.end(); ++it ) { // here problem implementation. need know // type of testbase derived class (testone) utilize // `changemode` method template parameter. //(*it)->changemode<aimodefollowline, sometype>(); } }; std::vector<std::shared_ptr<testbase>> vector_; }; i know bad design since vector_ filled @ runtime have no way of using changemode that. appears solution utilize multimethods, wouldn't ? if so, design ?
multimethods (aka multiple dispatch) deals issue of dispatching phone call single function based on runtime type of parameters involved. not appear issue (or have misunderstood you?), have 2 different method names, implemented on 2 different types.
your goal appears to select method implementation based on runtime type have injected class. not clear whether able dictate form injection takes if why not straight inject implementation? utilize implicit interface rather explicit one. in other words why not inject functor-like object?
class testbase { public: typedef std::function<void ()> ticker; testbase(ticker hbticker) : ticker{hbticker} {} void heartbeattick() { ticker(); } void setticker(ticker hbticker){ ticker = hbticker; } private: ticker ticker; }; seems lot less complicated me if meets requirements.
if need implement multiple dispatch need implement visitor pattern on each of parameters runtime type need determine. not sure if work multiple parameters though (i've not tried multiple parameters myself @ least). or utilize rtti , case statement or that.
c++ templates multiple-inheritance
No comments:
Post a Comment