开发者

C++: Cast to an interface that is not part of the base class

开发者 https://www.devze.com 2023-01-06 20:04 出处:网络
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 com开发者_如何学Cmon f

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 com开发者_如何学Cmon 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?


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


Someone posted a good explanation here.

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号