C ++ Saving Variables and Inheritance

Here is my situation:

I have an event driven system where all my handlers are retrieved from a class IHandlerand implement a method onEvent(const Event &event). Event is now the base class for all events and contains only an enumerated event type. All actual events are produced from it, including an event EventKeythat has 2 fields: (uchar) keyCodeand (bool) isDown.

Here's the interesting part: I am generating an event EventKeyusing the following syntax:

Event evt = EventKey(15, true);

and I send it to the handlers:

EventDispatch::sendEvent(evt); // void EventDispatch::sendEvent(const Event &event);

( EventDispatchcontains a linked list IHandlersand calls their method onEvent(const Event &event)with a parameter containing the sent event.

Now the actual question:

, , Event, ?

  • , .

  • , , , , EventKey, . - , , .

  • , , , , - - ? - , ?

, ?

+3
3

: . boost:: shareed_ptr boost:: dynamic_pointer_cast, .

+1

, , , :

Event evt = EventKey(15, true);

EventKey, Event, . , EventKey, . , , .

, (, , ) ... onEventA, onEventB ..

+4

, . - - .

If your implementation partially implemented C ++ 0x (for example, the latest versions of g ++ or VC ++ 2010), you can simply use std::queue<std::unique_ptr<Event>>. If std::unique_ptrnot available, you can use boost::ptr_dequeeither some other suitable container from the Boost Pointer Container library .

+1
source

Source: https://habr.com/ru/post/1747929/


All Articles