By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

Storing std::type_info

P: n/a
Hi all,

I tried to store a std::type_info value in my code so I didn't have to
call typeid more than once when checking if a polymorphic object is of
one of certain types, like this:

#include <typeinfo>

class A
{
public:
virtual int foo() { return 42; }
};

class B : public A
{};

class C : public A
{};

void foo(A& obj)
{
std::type_info type(typeid(obj));

if(type==typeid(B)) { ... }
if(type==typeid(C)) { ... }
}

I found, however, that the copy-constructor of type_info was private; is
it save to store the value by reference:

const std::type_info& type(typeid(obj)); ?

This compiles fine, but I'm not sure whether this stores the reference
to a temporary object which is no longer valid when the comparison happens.

Is is ok to do this with references? Or is there any other way to store
the type_info or do I have to insert typeid(obj) everywhere in the ifs?

Yours,
Daniel

PS: I know that RTTI should be uses as rarly as possible, please don't
tell me I should use something else. In my real situation, I believe
RTTI similar to this is what I want.

--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
Aug 11 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On 2007-08-11 16:02, Daniel Kraft wrote:
Hi all,

I tried to store a std::type_info value in my code so I didn't have to
call typeid more than once when checking if a polymorphic object is of
one of certain types, like this:

#include <typeinfo>

class A
{
public:
virtual int foo() { return 42; }
};

class B : public A
{};

class C : public A
{};

void foo(A& obj)
{
std::type_info type(typeid(obj));

if(type==typeid(B)) { ... }
if(type==typeid(C)) { ... }
}

I found, however, that the copy-constructor of type_info was private; is
it save to store the value by reference:

const std::type_info& type(typeid(obj)); ?

This compiles fine, but I'm not sure whether this stores the reference
to a temporary object which is no longer valid when the comparison happens.

Is is ok to do this with references? Or is there any other way to store
the type_info or do I have to insert typeid(obj) everywhere in the ifs?
Yes, a const reference can be bound to a temporary.

Or you can use & to get the address of the type_info object (typeid
returns a reference) but in that case you should use type_info's ==
operator and not compare addresses of type_info objects, i.e.
void foo(A& obj)
{
const std::type_info* type = &typeid(obj);

if(*type==typeid(B)) { std::cout << "B\n"; }
if(*type==typeid(C)) { std::cout << "C\n"; }
}

--
Erik Wikström
Aug 11 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.