Jump to content
  • Advertisement
Sign in to follow this  
JimPrice

RTTI question

This topic is 5044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to loop through a map to find if any particular instance (EntityBuilding) of a derived class (from Entity)exists, code snippet as follows:

for(std::map<unsigned int, Entity*>::iterator it = entityList_.begin(); it != entityList_.end(); ++it)
	{
		Entity* curEntity = &(*it->second);
		if(typeid(*curEntity) == typeid(EntityBuilding))
		{
				return true;
		}


However, it keeps on crashing on me, and I'm not sure why. Using VC2003, it's an unhandled exception. (And yes, I know I should use std::for_each!) Thanks, Jim.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by JimPrice
(And yes, I know I should use std::for_each!)


Well you wouldn't use for_each to do a search there are STL algorithms for that particular task, and you would probably been less likely to have had bugs if you had started off trying to use STL algorithms in the first place first [smile], anyways this code should do it for you (using boost smart pointers, can't be bothered with raw pointers to much work for no gain).


#include <boost\smart_ptr.hpp>
#include <typeinfo>
#include <algorithm>
#include <map>
#include <iostream>

struct b { virtual ~b() {} };
struct d : b {};
struct c : b {};
struct e : b {};

typedef boost::shared_ptr< b > b_ptr;

typedef std::map< unsigned int, b_ptr > bmap;

struct type_equals {
const std::type_info& info;

type_equals(const std::type_info& f): info(f) {}

bool operator()(const bmap::value_type& b) const {
return typeid(*b.second) == info;
}
};

int main() {

bmap m;

b_ptr foo(new d);
m[0] = foo;

foo.reset(new c);
m[1] = foo;

foo.reset(new e);
m[2] = foo;

foo.reset(new c);
m[3] = foo;

if(std::find_if(m.begin(), m.end(), type_equals(typeid(e))) != m.end())
std::cout << "found\n";
else
std::cout << "not found\n";

}


[Edited by - snk_kid on November 25, 2004 7:01:11 PM]

Share this post


Link to post
Share on other sites
Quote:

Well you wouldn't use for_each to do a search there are STL algorithms for that particular task, and you would probably been less likely to have had a bugs if you had started off trying to use STL algorithms in the first place first


OK, I feel sad now!!!

Thanks for the example - unfortunately I can't use find_if because the example I gave was simplified (I actually want to do an operation every time I get a type-hit in the map), but the code is a great step towards what I want.

Thanks!
Jim.

Share this post


Link to post
Share on other sites
Quote:
Original post by JimPrice
Quote:

Well you wouldn't use for_each to do a search there are STL algorithms for that particular task, and you would probably been less likely to have had a bugs if you had started off trying to use STL algorithms in the first place first


OK, I feel sad now!!!


Sorry!!!, wasn't meant to be an attack on you!.

Actually i remembered something, did you enable RTTI in VC++ 2003?

Share this post


Link to post
Share on other sites
Quote:

Sorry!, wasn't meant to be an attack on you!.


No worries, it was tongue in cheek!

Quote:

Actually i remembered something, did you enable RTTI in VC++ 2003?


Er - do you have to do this? This is the first time I've used RTTI in VC2003 (been using DevC++ until recently). I just assumed once you'd included the requisite headers then Bob was your proverbial uncle - are you saying that's not the case?

Off to check the help files in VC.

Jim.

Share this post


Link to post
Share on other sites
Quote:

it might do the trick


Yup, that seems to work. There is a brief mention of the extra flags in the typeid entry on the help engine.

Thanks for the help - would rate you up, but I can't rate you any higher!

Jim.

Edit : Do you know why RTTI isn't enabled by default?

Share this post


Link to post
Share on other sites
Quote:
Original post by JimPrice
Thanks for the example - unfortunately I can't use find_if because the example I gave was simplified (I actually want to do an operation every time I get a type-hit in the map), but the code is a great step towards what I want.


If you're acutally attempting an operation on the object if it's a certain type, then chances are that you should be using a dynamic_cast instead of typeid. That will determine if the object is of the correct type and get you a pointer to it all at the same time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!