Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store TypeInfo

Tags:

c++

  class A {}
  A a;
  type_info info = typeid (a); // error type_info is private

i want a list list<type_info> to store the type of classes. Is there a solution?

like image 802
jack-london Avatar asked Oct 16 '09 13:10

jack-london


People also ask

Where is Rtti stored?

RTTI for basic types such as int and bool is stored in the run-time library. Therefore, object files generated from a C++ program might reference RTTI defined in libc++abi.

What is include Typeinfo in C++?

std::type_info The class type_info holds implementation-specific information about a type, including the name of the type and means to compare two types for equality or collating order. This is the class returned by the typeid operator.


2 Answers

You can't create copies of 'type_info' objects. However, the result if 'typeid' is an Lvalue and the corresponding 'type_info' objects, once obtained, continue to live till the end of the program. For these reasons, you can safely store pointers to 'type_info' objects in your list.

like image 141
AnT Avatar answered Sep 23 '22 07:09

AnT


You cannot instantiate objects of the type_info class directly, because the class has only a private copy constructor. Since the list needs copy constructor...

If you really need it, use std::list< type_info*>.

I don't know why you need this list, but I would think to an alternative design, not involving RTTI, if possible.

like image 25
Cătălin Pitiș Avatar answered Sep 19 '22 07:09

Cătălin Pitiș