473,569 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7689
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==typei d(B)) { std::cout << "B\n"; }
if(*type==typei d(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
7391
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 type_info node" or "undefined reference to SomeClass type_info function" What does that really mean? What is the linker looking for? Thanks.
3
1492
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
1364
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 lots of errors to do with typeinfo and math.h for some reason. I have included the errors below - any help would be most appreciated. Thanks in...
19
3601
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 same type_info structure for a given T? My map would look like this: typedef std::map<const std::type_info &, ......> Type_map; Cheers,
2
6280
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 in the creation of classes derived from osg::Object, or osg::Node. The main reason these #MACROS are useful is because the cpp provides...
3
3373
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 template<typename T>
5
2559
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: const char *s = string_mapper<thetype>(); However I do not know the string to be associated with the type at compile time, and will need a way to set...
9
3359
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 design, class type_info { public: _CRTIMP virtual ~type_info();
4
2363
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 retrieve an instance of particular template 3). What type to return an instance as .. Assuming I have the following code:
0
7695
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.