Friday 15 February 2013

function pointers - How can I create a type based lookup table in order to implement multiple-dispatch in C++? -


I am trying to create a messaging system containing messages based on any class work "messageable" HandleMessage () is overloaded for example:

  class messageable (public: zero message taking) (message and message) {it-> Sending message (message); } Secure: Zero lock function (std :: type_info type, / * needs help here / / func) {m_handlers [type] = func; } Zero send message (messages and messages) {m_handlers [Typid (message)] (message); } Private: std :: map & lt; Std :: type_info, / * Need help here * / & gt; M_handlers; }; Class Test Messaging: Public Messageable (Public: Test Message) (This-> Bind Function (Typid (Visual Message), Zero (Test Messaging :: * Management Message); This-> Bind Function (Typid (dance message), zero (TestMessageable :: * Management) (dance message);} Conserved: Zero Handle Message (Visual Messaging Visual Message) {// Do something with visual message} Nine handset message (Dance message dance The message Sh) {// do something here with dance}}};  

In short, I want the correct version of the handle message based on the RTTI value of any message.

I look at the Double Dispatcher pattern without any type of monolithic switch / case statement

See information.

You should be able to implement VisualMessage as a class in this way:

  Class VI For dual message: public Message {public: virtual void dispatch (Message-qualified and message-qualified) {InMessageable.handleMessage (* this); }};  

And then call it like this:

  Message & amp; VMessage = Visual message (); Message worthy & amp; Tmessageable = TestMessageable (); VMessage.dispatch (tMessageable);  

This will then call TestMessage :: Message (visual message and visual message)

The reason for this is that the message: The sending view will be based on the message type. Then when VisualMessage :: Dispatch call in these messaging. Handle message (* This) will call the correct handle message because * This type of pointer is a visual message, no message.


No comments:

Post a Comment