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! 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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.
|
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...
|
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,
|
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...
| |
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>
|
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...
|
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();
|
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:
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |