I have a vector with a number of items, which are of two or more different class types (RaptorLayoutBox, RaptorLayoutSearchBox and RaptorLayoutPlasmoidArea). They all inherit from the same abstract class (RaptorLayoutAbstractItem).
I have three functions which searches the vector for a hit and should return the match. For example a searchBox() function that will search for a item of type RaptorLayoutSearchBox and return it.
I am trying to use typeid() to identify the type of the item. But it doesn't work too well. All items identifies as "24RaptorLayoutAbstractItem".
Here is the code, there is a lot of qt in there but it should make no difference. I was using QVector but I have changed it to std::vector, hoping that it would help. But no luck. Anyway:
Code:
std::vector<RaptorLayoutAbstractItem*> *v = new std::vector<RaptorLayoutAbstractItem*>();
v->push_back(new RaptorLayoutSearchBox("mama", this));
v->push_back(new RaptorLayoutPlasmoidArea("papa", this, 0, 0));
QString str1, str2;
str1.append("Item 0 is of type ").append(typeid(*(v->at(0))).name()).append(".\n");
str2.append("Item 1 is of type ").append(typeid(*(v->at(1))).name()).append(".\n");
QMessageBox::information(0, "Vector", QObject::tr("%1\n%2").arg(str1).arg(str2));
Result:
"Item 0 is of type 24RaptorLayoutAbstractItem.
Item 1 is of type 24RaptorLayoutAbstractItem."
Expected result:
"Item 0 is of type ##RaptorLayoutSearchBox.
Item 1 is of type ##RaptorLayoutPlasmoidArea."
(## = some number)