typeinfo / specifficaly typeid
Using C++, on unix through g++
I need some info on how to use the typeid() function. Any website indications would be great.
specifically, i have class b and c, which are both derived from class a.
in my main program, I use an iterator for a vector of a* to traipse the list, and i believe
if (typeid(*iter) == typeid(b))
{...}
else
{other stuff)
should pick out the objects of type b.
The iterator is pointing to the right objects, (checked with a display() funct, I have included the typeinfo lib.
always with b objects, other stuff is done instead of ...
I think what you are doing is correct. Maybe there is a compiler flag you need to set to turn on RTTI. I know there is such a flag in VC.
HTH
Doug Sutherland
HTH
Doug Sutherland
May 21, 2001 06:57 AM
I''m not sure your still looking for help on this but.
I''m not sure about the typeid, but in the past I''ve used dynamic_cast to achieve the same thing. Internally I guess dynamic_cast use the typeid information.
To use dynamic casts you need to do something like
Basically the dynamic cast returns a valid pointer if CGameObject* is actually a CPlayer ( CPlayer derived class ).
To use dynamic casts and ( I guess ) typeid the classes involved must have at least one virtual function. And as Doug said you''ll also need to set a compiler option enabling rtti.
Under djgpp (might be the same as unix g++) use
-frtti
Under VC++
/GR
Tom.
I''m not sure about the typeid, but in the past I''ve used dynamic_cast to achieve the same thing. Internally I guess dynamic_cast use the typeid information.
To use dynamic casts you need to do something like
class CGameObject;class CPlayer : public CGameObject;vector::iterator iter;iterating through a vector of CGameObject pointers you might useCPlayer* player = dynamic_cast(*iter);if ( player != NULL ){ //Process player specific code}
Basically the dynamic cast returns a valid pointer if CGameObject* is actually a CPlayer ( CPlayer derived class ).
To use dynamic casts and ( I guess ) typeid the classes involved must have at least one virtual function. And as Doug said you''ll also need to set a compiler option enabling rtti.
Under djgpp (might be the same as unix g++) use
-frtti
Under VC++
/GR
Tom.
TheHermit, I think that you should avoid to use the typeid() function for doing such a branching while you are swepping thru'' a list of items.
(in general, I think the typeid() function should always be avoided)
Just add a virtual "shouldIPickIt()" boolean class observed in class A and redefine it in class B and C so that is returns "true" if pointing to an instance of class B.
For example:
Hope it (still) helps.
[home page] [e-mail]
---
"Lifting shadows off a dream once broken
She can turn a drop of water into an ocean"
(in general, I think the typeid() function should always be avoided)
Just add a virtual "shouldIPickIt()" boolean class observed in class A and redefine it in class B and C so that is returns "true" if pointing to an instance of class B.
For example:
|
Hope it (still) helps.
[home page] [e-mail]
---
"Lifting shadows off a dream once broken
She can turn a drop of water into an ocean"
---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]
Weeelll, seems i found a satisfactory answer to my problem...
Someone suggested use (typeid(**iter) == typeid(b)), which, well, works.
Bugger of a thing is, have seen, and confirmed my initial way working in a different program, same system, compiler version, libraries, etc.
Only difference really is - that prog is all in one file, headers, classes, program, everything. Mine is in seperate files: a.h, b.h, a.cpp, b.cpp, main.h, main.cpp. Everything else works the initial way....
AAAAAAHHHHHHH!!!!!!!!
(oh well...)
Someone suggested use (typeid(**iter) == typeid(b)), which, well, works.
Bugger of a thing is, have seen, and confirmed my initial way working in a different program, same system, compiler version, libraries, etc.
Only difference really is - that prog is all in one file, headers, classes, program, everything. Mine is in seperate files: a.h, b.h, a.cpp, b.cpp, main.h, main.cpp. Everything else works the initial way....
AAAAAAHHHHHHH!!!!!!!!
(oh well...)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement