简体   繁体   English

C ++存储变量和继承

[英]C++ Storing variables and inheritance

Here is my situation: 这是我的情况:

I have an event driven system, where all my handlers are derived from IHandler class, and implement an onEvent(const Event &event) method. 我有一个事件驱动系统,我的所有处理程序都是从IHandler类派生的,并实现了一个onEvent(const Event &event)方法。 Now, Event is a base class for all events and contains only the enumerated event type. 现在,Event是所有事件的基类,仅包含枚举的事件类型。 All actual events are derived from it, including the EventKey event, which has 2 fields: (uchar) keyCode and (bool) isDown . 所有实际事件都是从它派生的,包括EventKey事件,它有2个字段: (uchar) keyCode(bool) isDown

Here's the interesting part: I generate an EventKey event using the following syntax: 这是有趣的部分:我使用以下语法生成EventKey事件:

Event evt = EventKey(15, true);

and I ship it to the handlers: 然后我将它发送给处理程序:

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

( EventDispatch contains a linked list of IHandlers and calls their onEvent(const Event &event) method with the parameter containing the sent event. EventDispatch包含IHandlers的链接列表,并使用包含已发送事件的参数调用其onEvent(const Event &event)方法。

Now the actual question: 现在的实际问题是:

Say I want my handlers to poll the events in a queue of type Event , how do I do that? 假设我希望我的处理程序轮询Event类型的队列中的Event ,我该怎么做?

  • Dynamic pointers with reference counting sound like too big of a solution. 带有引用计数声音的动态指针听起来太大了。

  • Making copies is more difficult than it sounds, since I'm only receiving a reference to a base type, therefore each time I would need to check the type of event, upcast to EventKey and then make a copy to store in a queue. 制作副本比听起来更困难,因为我只接收对基类型的引用,因此每次我需要检查事件类型,向上转换为EventKey ,然后制作副本以存储在队列中。 Sounds like the only solution - but is unpleasant since I would need to know every single type of event and would have to check that for every event received - sounds like a bad plan. 听起来像是唯一的解决方案 - 但是由于我需要了解每一种类型的事件并且必须检查每次收到的事件 - 听起来像一个糟糕的计划,这是令人不愉快的。

  • I could allocate the events dynamically and then send around pointers to those events, enqueue them in the array if wanted - but other than having reference counting - how would I be able to keep track of that memory? 我可以动态分配事件然后发送指向这些事件的指针,如果需要将它们排列在数组中 - 但除了引用计数之外 - 我如何能够跟踪那些内存? Do you know any way to implement a very light reference counter that wouldn't interfere with the user? 你知道如何实现一个不会干扰用户的非常轻的参考计数器吗?

What do you think would be a good solution to this design? 您认为这个设计的良好解决方案是什么?

Ok, first off, this line doesn't do what you think it does: 好的,首先,这条线没有按照你的想法做到:

Event evt = EventKey(15, true);

This creates a temporary EventKey object, slices it to the base class Event , then calls the Event copy constructor. 这将创建一个临时的EventKey对象, 将其切片到基类Event ,然后调用Event复制构造函数。 All actual data kept in the EventKey class is lost. 保存在EventKey类中的所有实际数据都将丢失。 If you want polymorphism, you want to use either dynamic objects or references. 如果需要多态,则需要使用动态对象或引用。

As for your actual question, I'd recommend you redesign and have separate queues (and thus separate handlers) for each event type... onEventA, onEventB, etc. 至于你的实际问题,我建议你重新设计并为每个事件类型... onEventA,onEventB等分别设置队列(以及独立的处理程序)。

Opinion: choose the reference counting option. 意见:选择引用计数选项。 Use boost::shareed_ptr, and boost::dynamic_pointer_cast to determine actual type. 使用boost :: shareed_ptr和boost :: dynamic_pointer_cast来确定实际类型。

You don't really need reference counting unless you share your event objects. 除非您共享事件对象,否则您不需要引用计数。 If you only have one owner at any given time - in your case, it would normally be the event queue - then you simply need to use an appropriate container. 如果您在任何给定时间只有一个所有者 - 在您的情况下,它通常是事件队列 - 那么您只需要使用适当的容器。

If your implementation has partially implemented C++0x already (eg recent g++ versions, or VC++2010), then you can simply use std::queue<std::unique_ptr<Event>> . 如果您的实现已经部分实现了C ++ 0x(例如最近的g ++版本或VC ++ 2010),那么您可以简单地使用std::queue<std::unique_ptr<Event>> If std::unique_ptr is not available, you can use boost::ptr_deque or some other suitable container from the Boost Pointer Container library . 如果std::unique_ptr不可用,您可以使用boost::ptr_dequeBoost Pointer Container库中的其他一些合适的容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM