Identify class type
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*>(); "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) |
typeid works correctly with "polymorphic classes". The following from ISO C++ Standard 5.2.8:
Quote:
Are you sure that RaptorLayoutAbstractItem contains a virtual destructor? Check this: Code:
#include <iostream> Quote:
|
All times are GMT -5. The time now is 08:55 AM. |