Note that there are some explanatory texts on larger screens.

plurals
  1. POBuild class hierarchy with overridable handler functions
    text
    copied!<p>I'm currently trying to build a class hierarchy automatically by using C++ templates. The final result is a message handler class that provides handler functions for all possible messages, given in a typelist.</p> <p>However when inheriting from that class hierarchy and trying to implement the handler function to actually do the application code <em>for some types only</em>, C++ isn't calling the function from the base class.</p> <p>The following is a minimal and complete example what I'm trying to achieve. It doesn't compile because C++ complains about not finding an overload for <code>handle( const B&amp; )</code>.</p> <pre><code>#include &lt;iostream&gt; #include &lt;typeinfo&gt; // Typelist. struct None {}; template &lt;class H, class T = None&gt; struct Typelist { typedef H Head; typedef T Tail; }; template &lt;class TL&gt; struct Handler : public Handler&lt;typename TL::Tail&gt; { using Handler&lt;typename TL::Tail&gt;::handle; virtual void handle( const typename TL::Head&amp; obj ) { std::cout &lt;&lt; "Not handled! " &lt;&lt; typeid( typename TL::Head ).name() &lt;&lt; std::endl; } }; template &lt;&gt; struct Handler&lt;None&gt; { virtual void handle() {} }; struct A {}; struct B {}; typedef Typelist&lt;A, Typelist&lt;B&gt; &gt; MsgList; struct MyHandler : Handler&lt;MsgList&gt; { void handle( const A&amp; a ) { std::cout &lt;&lt; "A!" &lt;&lt; std::endl; } }; int main() { MyHandler handler; A a; B b; handler.handle( a ); handler.handle( b ); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload