I created a simple program that demonstrates the runtime error that I get with my Qt application that uses multiple inheritance. The inheritance tree is as follows:
QGraphicsItem (abstract) \ QGraphicsLineItem MyInterface (abstract) \ / \ / MySubclass
And here is the code:
/* main.cpp */ #include <QApplication> #include <QGraphicsScene> #include <QGraphicsLineItem> //simple interface with one pure virtual method class MyInterface { public: virtual void myVirtualMethod() = 0; }; //Multiple inheritance subclass, simply overrides the interface method class MySubclass: public QGraphicsLineItem, public MyInterface { public: virtual void myVirtualMethod() { } }; int main(int argc, char** argv) { QApplication app(argc, argv); //init QApplication QGraphicsScene *scene = new QGraphicsScene(); //create scene scene->addItem(new MySubclass()); // add my subclass to the scene Q_FOREACH(QGraphicsItem *item, scene->items()) // should only have one item { MyInterface *mInterface = (MyInterface*)item; // cast as MyInterface mInterface->myVirtualMethod(); // <-- this causes the error } return 0; }
Debugging in visual studio results in a runtime error when invoking an interface method:
Run-Time Check Failure
Any idea what the problem is?
Ben burnett
source share