-
C++, RTTI and vectors; getting the exact type
Hello,
I have a base class that derives two childs.
class A {};
class B : public A {};
class C : public A {};
I have another class that has a pointer to collection of class A members using a vector, something like this:
vector<*A> *m_collection;
And what I do is to create objects of class B or C and add the to the collection using push_back:
B *myb = new B();
m_collection->push_back(myb);
Then I loop the collection and try to check using 'typeid' but it always return the base class (A). Is it not possible to know the exact type?
Thank you!!!
-
For typeid to be evaluated at run-time, it must be applied to a reference to a polymorphic class type.
This means, class A must be a polymorphic type - ie. it must have at least one virtual function. for example,
Code:
class A { public: virtual ~A() {} /* .... */ };
typeid must be applied to a reference (not pointer). for example,
Code:
A* pointer = m_collection[i] ;
if( pointer != 0 )
{
A& reference = *pointer ;
const std::type_info& typeinfo = typeid(reference) ;
}
This would give you the type_info corrosponding to the run-time type of the object.
-
One tint nit: your vector needs to be declared like this:
vector<A*> m_collection;
There's no need to use m_collection as a pointer, unless you want to pass it to a function.
Danny Kalev
-
Since you seem to be a learner (as am I), let me be so bold as to make a suggestion. Rarely should you need typeid or dynamic_cast to determine if the object is from a derived class. That is, you should rarely want to know. The base class pointer (which may point to an object of a derived class) should behave correctly without first interrogating it.
-
 Originally Posted by hendrixj
Since you seem to be a learner (as am I), let me be so bold as to make a suggestion. Rarely should you need typeid or dynamic_cast to determine if the object is from a derived class. That is, you should rarely want to know. The base class pointer (which may point to an object of a derived class) should behave correctly without first interrogating it.
Thanks for you advice. I am a learner too. But I don't really see the point why should I not interrogate an object which type it really is.
 Originally Posted by Danny
One tint nit: your vector needs to be declared like this:
vector<A*> m_collection;
There's no need to use m_collection as a pointer, unless you want to pass it to a function.
True, thanks. Yes, in the "real" code I will need to pass it to a function. I tend to use pointers all the time. I don't know if this is not a good practice. Should the code have an excess of pointers or the other way around?
 Originally Posted by vijayan
For typeid to be evaluated at run-time, it must be applied to a reference to a polymorphic class type.
This means, class A must be a polymorphic type - ie. it must have at least one virtual function. for example,
Code:
class A { public: virtual ~A() {} /* .... */ };
typeid must be applied to a reference (not pointer). for example,
Code:
A* pointer = m_collection[i] ;
if( pointer != 0 )
{
A& reference = *pointer ;
const std::type_info& typeinfo = typeid(reference) ;
}
This would give you the type_info corrosponding to the run-time type of the object.
THANK YOU!!! I worked fine!!!
-
There will be rare times when you need to, which is why the capability is provided. Generally, however, a base class has virtual functions because you want it to behave polymorphically, e.g.
Code:
// Given that Plane and Bird are classes derived from Winged with a virtual function fly()
Plane plane(...);
Bird bird(...);
Winged* winged[2] = { &plane, &bird };
winged[0]->fly();
winged[1]->fly();
I don't need to know what kind of Winged winged[i] is, I just want it to fly like it is supposed to. If I need to know, there's a least a decent chance that I didn't need the virtual functions.
-
i don't think it's a good practice to put pointers into a vector without first ensuring they are handled correctly; furthermore, using RTTI to determine an object's type IMHO sort of defeats the idea of polymorphism -- your code should be designed so it doesn't have to ask: "what type are you", the beauty of polymorphism is that the appropriate member function is called automatically and determined during runtime.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|