|
Type information type
Stores information of a type.
This is the type returned by the typeid operator. It can be used to compare the type of objects or to retrieve the name of a type.
typeid can be applied directly to types, in which case it returns its information; Or to objects, in which case it returns information on the type of the object.
When typeid is applied to a dereferenced pointer to an object of a polymorphic class type (a class declaring or inheriting a virtual function), it considers its dynamic type (i.e., the type of the most derived object). This requires the RTTI (Run-time type information) to be available.
When typeid is applied to a dereferenced null pointer a bad_typeid exception is thrown.
It is declared as:
1 2 3 4 5 6 7 8 9 10 11
|
class type_info {
public:
virtual ~type_info();
bool operator== (const type_info& rhs) const;
bool operator!= (const type_info& rhs) const;
bool before (const type_info& rhs) const;
const char* name() const;
private:
type_info (const type_info& rhs);
type_info& operator= (const type_info& rhs);
};
|
Members
- operator==
operator!=
- Comparison operators. They return whether the two types describe the same type.
A derived type is not considered the same type as any of its base classes.
- before
- Returns true if the type precedes the type of rhs in the collation order.
The collation order is just an internal order kept by a particular implementation and is not necessarily related to inheritance relations or declaring order.
- name
- Returns a null-terminated character sequence with a human-readable name for the type.
- copy constructor and copy operator
- These are private, preventing type_info values from being copiable.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
// type_info example
#include <iostream>
#include <typeinfo>
using namespace std;
struct Base {};
struct Derived : Base {};
struct Poly_Base {virtual void Member(){}};
struct Poly_Derived: Poly_Base {};
int main() {
// built-in types:
int i;
int * pi;
cout << "int is: " << typeid(int).name() << endl;
cout << " i is: " << typeid(i).name() << endl;
cout << " pi is: " << typeid(pi).name() << endl;
cout << "*pi is: " << typeid(*pi).name() << endl << endl;
// non-polymorphic types:
Derived derived;
Base* pbase = &derived;
cout << "derived is: " << typeid(derived).name() << endl;
cout << " *pbase is: " << typeid(*pbase).name() << endl;
cout << boolalpha << "same type? ";
cout << ( typeid(derived)==typeid(*pbase) ) << endl << endl;
// polymorphic types:
Poly_Derived polyderived;
Poly_Base* ppolybase = &polyderived;
cout << "polyderived is: " << typeid(polyderived).name() << endl;
cout << " *ppolybase is: " << typeid(*ppolybase).name() << endl;
cout << boolalpha << "same type? ";
cout << ( typeid(polyderived)==typeid(*ppolybase) ) << endl << endl;
}
|
Possible output:
int is: int
i is: int
pi is: int *
*pi is: int
derived is: struct Derived
*pbase is: struct Base
same type? false
polyderived is: struct Poly_Derived
*ppolybase is: struct Poly_Derived
same type? true
|
See also
bad_typeid | Exception thrown on typeid of null pointer (class) |
|