473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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!
Aug 11 '07 #1
1 7658
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: sks_cpp | last post by:
What is a type_info function, more particularly, what is a type_info node? Are these related to the vtable by any means? I have seen linker errors such as: "undefined reference to SomeClass...
3
by: Bobo | last post by:
Hello all. Can anybody explay WTH std::type_info has a virtual destructor? AFAIK the constructor is private so you cannot inherit from it (who would want, anyway?). TIA. Bobo.
0
by: Tom McCallum | last post by:
Hi everyone, I am trying to compile a bison tab.cc file in with a c++ program I am writing. The program compiles fine without the parser attached but when I compile with it, it comes up with...
19
by: Marco Jez | last post by:
Hi everyone! I would like to use the reference returned by typeid as key in a std::map. Is it safe to assume that typeid(T) (where T is a type name) will always return the same reference to the...
2
by: Steven T. Hatton | last post by:
I've been working a lot with OpenSceneGraph lately. Overall it is very much in the spirit of C++. One feature OSG has which is, IMO, an eyesore, is the presence of a couple of #MACROs to assist...
3
by: Misiu | last post by:
Hello everybody, How to avoid using struct IsDataType for comparison for find_if algorithm but use something like that what is now commented out in the code below? Regards, Misiu ...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
9
by: Rahul | last post by:
Hi Everyone, I was working with Run Time Type Information and with typeid operator, and so on i tried to create an object of type_info class and i'm not able to do so, because of the class...
4
by: Bit Byter | last post by:
I want to write a (singleton) container for instances of my class templates, however, I am not too sure on how to: 1). Store the instances 2). How to write the acccesor method (instance()) to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.