Storing std::type_info
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!