Typeid
Encyclopedia
In C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

, the typeid keyword is used to determine the class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

 of an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 at run time. It returns a reference
Reference (C++)
In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C...

 to std::type_info object, which exists until the end of the program. The use of typeid is often preferred over dynamic_cast<class_type> in situations where just the class information is needed, because typeid, applied on a type or non de-referenced value is a constant-time procedure, whereas dynamic cast
Dynamic cast
In the C++ programming language, the dynamic_cast operator is a part of the run-time type information system that performs a typecast. Unlike an ordinary C-style typecast, a type safety check is performed at runtime, and if the types are not compatible, an exception will be thrown or a null...

must traverse the class derivation lattice of its argument at runtime. However, you should not rely on the exact content, such as that returned by std::type_info::name, as this is implementation specific with respect to the compiler.

Objects of class std::bad_typeid are thrown when typeid is called on a dereferenced null pointer. The class is derived from std::exception, and thrown by typeid and others.

It is generally only useful to use typeid on the dereference of a pointer or reference (i.e. typeid(*ptr) or typeid(ref)) to an object of polymorphic class type (a class with at least one virtual function
Virtual function
In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature...

) because these are the only expressions that are associated with run-time type information. The type of any other expression is known at compile time.

Example

  1. include
  2. include //for 'typeid'


class Person {
public:
// ... Person members ...
virtual ~Person {}
};

class Employee : public Person {
// ... Employee members ...
};

int main
{
Person person;
Employee employee;
Person *ptr = &employee;
Person &ref = employee;
// The string returned by typeid::name is implementation-defined
std::cout << typeid(person).name << std::endl; // Person (statically known at compile-time)
std::cout << typeid(employee).name << std::endl; // Employee (statically known at compile-time)
std::cout << typeid(ptr).name << std::endl; // Person * (statically known at compile-time)
std::cout << typeid(*ptr).name << std::endl; // Employee (looked up dynamically at run-time
// because it is the dereference of a
// pointer to a polymorphic class)
std::cout << typeid(ref).name << std::endl; // Employee (references can also be polymorphic)
}


Output (exact output varies by system):

Person
Employee
Person*
Employee
Employee
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK