The typeid operator allows the type of an object to be determined at run time. The result of typeid is a const type_info& . The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used.
The typeid operator returns an lvalue of type const std::type_info that represents the type of expression expr. You must include the standard template library header <typeinfo> to use the typeid operator.
Just like dynamic_cast, the typeid does not always need to use RTTI mechanism to work correctly. If the argument of the typeid expression is non-polymorphic type, then no runtime check is performed. Instead, the information about the type is known at the compile-time.
Since typeid is applied to a type rather than an object, there is no runtime type information, so that overhead won't be a problem.
I have a list of pointers to objects. These objects have nothing in common (i.e. no common base class); for better understanding: It is a list of objects that lie under the mouse cursor in a GUI.
Now I would like to know what kind of object it is. A node, a node handle, a line segment, a tag, and so on. However I cannot use typeid(*ptr)
since ptr is a const void*
.
Any solution for this? Can I force the usage of typeid since I know that the pointers always point to objects and not to mere values? Or is there no way around adding some fake common base class?
(edit: Currently I'm doing it that way that I store a struct in the list which additionally stores the type of the object (as enum). Maybe I should change this to store a type_info
object ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With