Note that there are some explanatory texts on larger screens.

plurals
  1. POgiven abstract base class X, how to create another template class D<T> where T is the type of the class deriving from X?
    primarykey
    data
    text
    <p>I want to be able to accept a <code>Message&amp;</code> object which references either a <code>Message1</code> or <code>Message2</code> class. I want to be able to create a <code>MessageWithData&lt;Message1&gt;</code> or <code>MessageWithData&lt;Message2&gt;</code> based on the underlying type of the <code>Message&amp;</code> object. For example, see below:</p> <pre><code>class Message {}; class Message1 : public Message {}; class Message2 : public Message {}; template&lt;typename Message1or2&gt; class MessageWithData : public Message1or2 { public: int x, y; } class Handler() { public: void process(const Message&amp; message, int x, int y) { // create object messageWithData whose type is // either a MessageWithData&lt;Message1&gt; or a MessageWithData&lt;Message2&gt; // based on message's type.. how do I do this? // messageWithData.dispatch(...) } }; </code></pre> <p>The messageWithData class essentially contains methods inherited from Message which allow it to be dynamically double dispatched back to the handler based on its type. My best solution so far has been to keep the data separate from the message type, and pass it all the way through the dynamic dispatch chain, but I was hoping to come closer to the true idiom of dynamic double dispatch wherein the message type contains the variable data.</p> <p>(The method I'm more or less following is from <a href="http://jogear.net/dynamic-double-dispatch-and-templates" rel="nofollow">http://jogear.net/dynamic-double-dispatch-and-templates</a>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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