在学习java中的Class类,有getClass方法,其特性与c++中的typeid很类似,java中调用getClass方法和c++中的typeid运算符等价。
typeid是C++的一个关键字,其作用是在程序运行时,返回对象的类型,即所谓的RTTI(Run Time Type Identification), 它使程序能够获取由指针或引用所指向的对象的实际类型,即允许“用指向基类的指针或引用来操作对象”的程序能够获取到“这些指针或引用所指对象”的实际派生类型。在C++中,为了支持RTTI提供了两个操作符:dynamic_cast和typeid。
typeid操作符的返回结果是名为type_info的标准库类型的对象的引用。 type_info在typeinfo头文件中定义:
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);
};
如下内容摘自http://www.cplusplus.com/reference/typeinfo/type_info/:
typeid can be applied to any typed expression, including a type itself, to retrieve the its type_info.
When typeid is applied to a reference or 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.
type_info成员介绍如下:
- 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.
示例代码:
07 | struct Derived : Base {}; |
08 | struct Poly_Base { virtual void Member(){}}; |
09 | struct Poly_Derived: Poly_Base {}; |
15 | cout << "int is: " << typeid ( int ).name() << endl; |
16 | cout << " i is: " << typeid (i).name() << endl; |
17 | cout << " pi is: " << typeid (pi).name() << endl; |
18 | cout << "*pi is: " << typeid (*pi).name() << endl << endl; |
22 | Base* pbase = &derived; |
23 | cout << "derived is: " << typeid (derived).name() << endl; |
24 | cout << " *pbase is: " << typeid (*pbase).name() << endl; |
25 | cout << boolalpha << "same type? " ; |
26 | cout << ( typeid (derived)== typeid (*pbase) ) << endl << endl; |
29 | Poly_Derived polyderived; |
30 | Poly_Base* ppolybase = &polyderived; |
31 | cout << "polyderived is: " << typeid (polyderived).name() << endl; |
32 | cout << " *ppolybase is: " << typeid (*ppolybase).name() << endl; |
33 | cout << boolalpha << "same type? " ; |
34 | cout << ( typeid (polyderived)== typeid (*ppolybase) ) << endl << endl; |
输出:
06 | derived is: struct Derived |
07 | *pbase is: struct Base |
10 | polyderived is: struct Poly_Derived |
11 | *ppolybase is: struct Poly_Derived |