Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
typeid operator is returning const type_info&... but in the type_info class implementation copy constructor and operator= is in private mode.. how it is internally creating object and returning reference to us? can any one explain me about this ?
Posted
Updated 9-May-11 23:33pm
v2

How typeid is implemented is, precisely, a implementation detail hence compiler-specific and, generally hidden to the C++ developer. You may find that RTTI (and more generally all the reflection) facilities usually happily ignore the relative programming language access rules.
 
Share this answer
 
Comments
Eliteknites 10-May-11 4:52am    
Fine.. how internally object is created to type_info? wheather it uses singleton mechanism? or something else ah?
CPallini 10-May-11 4:54am    
I thought 'hidden to the C++ developer' was a hint.
Olivier Levrey 10-May-11 8:15am    
Have my 5.
CPallini 10-May-11 8:19am    
Thank you.
Nish Nishant 10-May-11 8:22am    
Good answer. Voted 5!
Maybe - by default constructor ? :)
 
Share this answer
 
typeid is a keyword exactly like sizeof in native C/C++. It is not a function or a class. Therefore, there is no copy-constructor, nor = operator behind this.

CPallini's answer should be enough: you can't know how it works internally and even don't need to.

i am asking how typeid is internally creating object for the type_info class?

From MSDN: You cannot instantiate objects of the type_info class directly, because the class has only a private copy constructor. The only way to construct a (temporary) type_info object is to use the typeid operator. Since the assignment operator is also private, you cannot copy or assign objects of class type_info.

But they don't give details on how they instanciate the object (why would they actualy?). Anyway, there are several ways to achieve this. For example you can make your constructor internal. This makes instanciation possible only from the assembly where the type is defined.
 
Share this answer
 
v3
Comments
Eliteknites 10-May-11 8:31am    
typeid is keyword only.. but it returning type_info reference.. i am talking about type_info class.. in that class implementation copy constructor and operator= is in private mode.. so it is not possible to create a object for that class.. i am asking how typeid is internally creating object for the type_info class?
Olivier Levrey 11-May-11 4:03am    
I updated my answer. Please see.
Since you get a reference in return, the object must exist statically. So it's most likely this is technically a singleton object, although it may not be implemented as such. (but the private assignment and constructor indicate that it is).

There are typically two ways to create a singleton instance:

1. either you provide a static function in the class that does it for you - as a class function it can access the private constructor.

2. or you create a static variable outside of the class. But with the constructor private, this requires either a friend declaration or some nastier tricks, e. g. by using malloc to allocate it and memcpy to 'assign' the member variables. I wouldn't be surprised if that is what MS did. But we'll never know ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900