C++:转换为不属于基类的接口
我有一系列表示“智能”地图元素的类:MapTextElement
、MapIconElement
等。这些类扩展了各种 Qt 图形项类,但也提供了通用功能,例如作为一个抽象工厂方法,返回专门用于每个类的属性面板。我已在纯虚拟类 MapElementInterface
中声明了这些常用方法。然后,我的类会多重继承适当的 Qt 基类以及接口:
class MapTextElement : public QGraphicsTextItem, public MapElementInterface
class MapIconElement : public QGraphicsItem, public MapElementInterface
所以我的类层次结构看起来有点像:
+-------------+ +-------------------+
|QGraphicsItem| |MapElementInterface|
+-------------+ +-------------------+
^ ^ ^
| | |
+------+------+ | |
| | | |
+-----------------+ +--------------+ |
|QGraphicsTextItem| |MapIconElement| |
+-----------------+ +--------------+ |
^ |
| |
+-------------------+ +-----+
| |
+--------------+
|MapTextElement|
+--------------+
我从 Qt 提供的方法接收到一个指向 QGraphicsItem
的指针。在这种情况下,我知道指针不仅是QGraphicsItem
,而且是MapElementInterface
。我想将指针视为 MapElementInterface
。
QList<QGraphicsItem*> selected = scene_->selectedItems();
if (selected.count() == 1) {
// We know that the selected item implements MapEditorInterface
MapElementInterface *element = SOME_CAST_HERE<MapElementInterface*>(selected[0]);
QWidget *panel = element->GeneratePropertyPanel(property_dock_);
}
正确使用的演员阵容是什么?或者我的处理方式完全错误吗?
I have a series of classes representing "smart" map elements: MapTextElement
, MapIconElement
, etc. The classes are extending various Qt graphics item classes, but also provide common functionality, such as an abstract factory method that returns a property panel specialized for each class. I have declared these common methods in a pure virtual class, MapElementInterface
. My classes then multiply-inherit the appropriate Qt base class as well as the interface:
class MapTextElement : public QGraphicsTextItem, public MapElementInterface
class MapIconElement : public QGraphicsItem, public MapElementInterface
So my class hierarchy looks kind of like:
+-------------+ +-------------------+
|QGraphicsItem| |MapElementInterface|
+-------------+ +-------------------+
^ ^ ^
| | |
+------+------+ | |
| | | |
+-----------------+ +--------------+ |
|QGraphicsTextItem| |MapIconElement| |
+-----------------+ +--------------+ |
^ |
| |
+-------------------+ +-----+
| |
+--------------+
|MapTextElement|
+--------------+
I am receiving a pointer to a QGraphicsItem
from a Qt-provided method. In this case, I know that the pointer is not only QGraphicsItem
, but also MapElementInterface
. I want to treat the pointer as a MapElementInterface
.
QList<QGraphicsItem*> selected = scene_->selectedItems();
if (selected.count() == 1) {
// We know that the selected item implements MapEditorInterface
MapElementInterface *element = SOME_CAST_HERE<MapElementInterface*>(selected[0]);
QWidget *panel = element->GeneratePropertyPanel(property_dock_);
}
What is the proper cast to use? Or am I going about this completely the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于多重继承,dynamic_cast 是唯一的方法,并检查返回值是否为 NULL。
With multiple inheritance, dynamic_cast is the only way, and check the return value against NULL.
有人在这里发布了很好的解释。
什么时候应该使用static_cast、dynamic_cast、const_cast和reinterpret_cast使用过吗?
Someone posted a good explanation here.
When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
这里必须小心,因为给定图表,您的指针可能指向 MapTextElement 或 MapIconElement。这里唯一安全的选择是使用动态铸造。或者为自己提供另一种方式来确定对象的类型。
You have to be careful here, because given the diagram your pointer could point to either MapTextElement or MapIconElement. The only safe bet for you here is to go with dynamic casting. Or provide another way for yourself to figure the object's type.
您可以转换为 MapIconElement,然后转换为 MapElementInterface,也可以转换为 MapTextElement,然后转换为 MapElementInterface。您必须选择一条路径(或动态向下强制转换以检查您采用的路径)。
You can cast to MapIconElement, and then to MapElementInterface, or you can cast to MapTextElement, then to MapElementInterface. You must chose a path (or dynamic-down cast to check what path you take).