#include <typeinfo>#include <iostream>using namespace std;struct A{int a;int b;};void main(){A b;b.a = 3;b.b = 4;int a = 4;cout << typeid(a).name() << endl;cout << typeid(b).name() << endl;}
The following reproduced from: http://blog.programfan.com/article.asp? Id = 45931
Http://www.cppblog.com/smagle/archive/2010/05/14/115286.html
The typeid expression of the typeid operator is like: typeid (expr); expr is an arbitrary expression or type name. If the expression type is a class type and contains at least one virtual function, the typeid operator returns the dynamic type of the expression, which must be calculated at runtime. Otherwise, the typeid operator returns the static type of the expression, it can be computed during compilation. The typeid operator returns a reference to an object of the standard library type named type_info (defined in the header file typeinfo ). The standard does not define type_info exactly. Its exact definition is related to the compiler, but the standard specifies that its implementation must provide the following four operations:
T1 = t2 |
If the T1 and T2 objects are of the same type, true is returned; otherwise, false is returned. |
T1! = T2 |
If the T1 and T2 types of the two objects are different, true is returned; otherwise, false is returned. |
T. Name () |
Returns the C-style string of the type. The type name is generated using a system-related method. |
T1.before (T2) |
Returns the bool value indicating whether T1 exists before T2. |
The type_info class provides public virtual destructor so that users can use it as the base class. Its default constructor, copy constructor, and value assignment operator are both defined as private. Therefore, objects of the type_info type cannot be defined or copied. The only way to create a type_info object in the program is to use the typeid operator (it can be seen that if typeid is treated as a function, it should be the friend of type_info ). The name member function of type_info returns the C-style string to indicate the corresponding type name, however, it is important to note that the returned type name is not necessarily consistent with the corresponding type name used in the Program (this is often the case, see the program below), which is determined by the implementation, the standard only requires that a unique string be returned for each type. For example:
#include<iostream>
using namespacestd;
class Base {}; class Derived:public Base {};
int main() { cout <<typeid(int).name()<< endl <<typeid(unsigned).name()<< endl <<typeid(long).name()<< endl <<typeid(unsignedlong).name()<< endl <<typeid(char).name()<< endl <<typeid(unsignedchar).name()<< endl <<typeid(float).name()<< endl <<typeid(double).name()<< endl <<typeid(string).name()<< endl <<typeid(Base).name()<< endl <<typeid(Derived).name()<< endl <<typeid(type_info).name()<< endl; return 0; }
|
Running result in mingw2.05: I J L M C H F D SS 4 Base 7 derived St9type_info Terminated with return code 0 Press any key to continue... |
Note: If the pointer P is 0 when typeid is applied to x P, if P points to a class type with a virtual function, typeid (* P) throws a bad_typeid exception at runtime. Otherwise, the result of typeid (* P) is irrelevant to the value of P and can be determined during compilation. The typeid expression is similar but different from the sizeof expression. sizeof must be calculated during compilation. That is to say, only the static type of the expression is considered, it is independent of the dynamic type of the expression (even if a virtual function exists ). References: [1] C ++ primer (Edition 4)
[2] thinking in C ++ (Edition 2)
[3] international standard: ISO/IEC 14882: 1998