Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a visitor pattern.</p> <p>but a visitor should know each of subtypes and define an action for it, so no default action, AFAIK</p> <pre><code>class Visitor; class BaseMsg { //.. public: virtual void acceptVisitor(Visitor * v) = 0; }; class Msg1; class Msg2; class Visitor { // You can put here pure virtuals for sure every visitor will implement them public: virtual void action (Msg1 * msg) = 0; virtual void action (Msg2 * msg) = 0; }; class Msg1: public BaseMsg { //.. public: void acceptVisitor(Visitor * v){v-&gt;action(this);} }; class Msg2: public BaseMsg { //.. public: void acceptVisitor(Visitor * v){v-&gt;action(this);} }; class Visitor1 : public Visitor { // ... public: void action (Msg1 * msg) {/*...*/ cout &lt;&lt; "I like the message!\n";} void action (Msg2 * msg) {/*...*/ cout &lt;&lt; "I hate the message!\n";} // more messages and actions for them }; class Visitor2 : public Visitor{ // ... public: void action (Msg1 * msg) {/*...*/ cout &lt;&lt; "Just fine\n";} void action (Msg2 * msg) {/*...*/ cout &lt;&lt; "Sorry, I'm busy\n";} // more messages and actions for them }; int main() { BaseMsg * a = new Msg1; BaseMsg * b = new Msg2; Visitor * act = new Visitor1; Visitor * lazy = new Visitor2; // ............ // somewhere in a deep deep forest of your code a-&gt;acceptVisitor(act); b-&gt;acceptVisitor(act); // somewhere else a-&gt;acceptVisitor(lazy); b-&gt;acceptVisitor(lazy); delete act; delete lazy; delete a; delete b; return 0; } </code></pre> <p>Output:</p> <ul> <li>I like the message!</li> <li>I hate the message!</li> <li>Just fine</li> <li>Sorry, I'm busy</li> </ul>
    singulars
    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.
    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