C++:转换为不属于基类的接口

发布于 2024-09-09 12:00:42 字数 1740 浏览 3 评论 0原文

我有一系列表示“智能”地图元素的类:MapTextElementMapIconElement 等。这些类扩展了各种 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丶视觉 2024-09-16 12:00:42

对于多重继承,dynamic_cast 是唯一的方法,并检查返回值是否为 NULL。

With multiple inheritance, dynamic_cast is the only way, and check the return value against NULL.

刘备忘录 2024-09-16 12:00:42

这里必须小心,因为给定图表,您的指针可能指向 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.

书信已泛黄 2024-09-16 12:00:42

您可以转换为 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文