c++ - Subclass object as a parameter to a virtual function -


ok, have class in use in class which, among other things, asks on object report on relationship object of same type. works great. fyi class represents protocol. unfortunately, i'm trying decorate class relationship adding class called wrapper. main power of class can take first class argument (or else same).:

template< class innerinterpreter > class wrapperinterpreter { public:     wrapperinterpreter(const innerinterpreter &m) : innermessage(m) {}      ...      virtual bool respondstomessage(const wrapperinterpreter<innerinterpreter> &) const = 0;  public:     //member     innerinterpreter innermessage; }; 

what i've concluded other design reasons having base class set of wrappers (rather taking 2 different protocols arguments) allows me combinatorial set of outer , inner protocol classes.

so here's problem: when try subclass wrapperinterpreter<>, , in particular provide implementation respondstomessage(), end subclass able compare class in wrapperinterpreter heirarchy. that's not want do. force subclass implement method thusly:

template< class inner > class concretewrapper : wrapperinterpreter<inner> {     ...     bool respondstomessage(const concretewrapper<inner> &);     ... } 

one solution came remove interface of wrapperinterpreter , let compiler complain missing interface method when in use rest of design.

so question is: is there way put method in abstract class's interface, such subclass must take object of subclass type parameter?

alternatively, trying use inheritance incorrectly? have useful design patterns might me address problem?

there design called crtp (curiously recurring template pattern) consists in passing type of child parent template parameter:

template <typename child> class wrapperinterpreter {     using inner = typename child::inner; // nested typedef      wrapperinterpreter(inner const& ...);      virtual bool respondstomessage(child const& ...) = 0; };  template <typename inner> class concrete: public wrapperinterpreter< concrete<inner> > {      virtual bool respondstomessage(concrete const& ...) override;  }; 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -