typeid 是 C++ 的关键字之一,用于获取运行时类型信息,typeid操作符的返回结果是名为type_info的标准库类型的对象的引用(在头文件typeinfo中定义)。
上测试代码:
1#include <assert.h> 2#include <iostream> 3#include <string> 4#include <typeinfo> 5#include <vector> 6#include <cstdio> 7 8using namespace std; 9 10int func(int a) { 11 return 0; 12} 13 14typedef int(*fun_ptr)(int); 15 16class Base { 17public: 18 Base() {} 19}; 20 21int test_typeid1() { 22 char char_ = 'a'; 23 unsigned char uchar_ = 'b'; 24 short short_ = -16; 25 unsigned short ushort_ = 16; 26 int int_ = -1024; 27 unsigned int uint_ = 1024; 28 float float_ = 1.2f; 29 double double_ = 2.34; 30 long long_ = -12222; 31 long long llong_ = 12222; 32 int array[10] = { 0 }; 33 int* array_ptr = array; 34 string string_("xiongwei"); 35 vector<int> int_vector; 36 fun_ptr f = func; 37 Base base_; 38 Base* pBase = new Base; 39 Base& rBase = base_; 40 41 cout << "char_ type: " << typeid(char_).name() << std::endl; 42 assert(typeid(char).name() == typeid(char_).name()); 43 44 std::cout << "uchar type: " << typeid(uchar_).name() << std::endl; // uchar type: unsigned char 45 assert(typeid(unsigned char).name() == typeid(uchar_).name()); 46 47 std::cout << "short_ type: " << typeid(short_).name() << std::endl; // short_ type: short 48 assert(typeid(short).name() == typeid(short_).name()); 49 50 std::cout << "ushort_ type: " << typeid(ushort_).name() << std::endl; // ushort_ type: unsigned short 51 assert(typeid(unsigned short).name() == typeid(ushort_).name()); 52 53 std::cout << "int_ type: " << typeid(int_).name() << std::endl; // int_ type: int 54 assert(typeid(int).name() == typeid(int_).name()); 55 56 std::cout << "uint_ type: " << typeid(uint_).name() << std::endl; // uint_ type: unsigned int 57 assert(typeid(unsigned int).name() == typeid(uint_).name()); 58 59 std::cout << "float_ type: " << typeid(float_).name() << std::endl; // float_ type: float 60 assert(typeid(float).name() == typeid(float_).name()); 61 62 std::cout << "double_ type: " << typeid(double_).name() << std::endl; // double_ type: double 63 assert(typeid(double).name() == typeid(double_).name()); 64 65 std::cout << "long_ type: " << typeid(long_).name() << std::endl; // long_ type: long 66 assert(typeid(long).name() == typeid(long_).name()); 67 68 std::cout << "llong_ type: " << typeid(llong_).name() << std::endl; // llong_ type: __int64 69 assert(typeid(long long).name() == typeid(llong_).name()); 70 71 std::cout << "array[] type: " << typeid(array).name() << std::endl; // array[] type: int [10] 72 assert(typeid(int[10]).name() == typeid(array).name()); 73 74 std::cout << "array_header type: " << typeid(array_ptr).name() << std::endl; // array_header type: int * __ptr64 75 assert(typeid(int*).name() == typeid(array_ptr).name()); 76 77 std::cout << "string_ type: " << typeid(string_).name() << std::endl; // string_ type: class std::basic_string<char,struct std::char_traits<char>, class std::allocator<char>> 78 assert(typeid(std::string).name() == typeid(string_).name()); 79 80 std::cout << "int_vector type: " << typeid(int_vector).name() << std::endl; // int_vector type: class std::vector<int,class std::allocator<int>> 81 assert(typeid(std::vector<int>).name() == typeid(int_vector).name()); 82 83 std::cout << "f type: " << typeid(f).name() << std::endl; // f type : int(__cdecl*)(int) 84 assert(typeid(int(*)(int)).name() == typeid(f).name()); 85 86 std::cout << "Base_ type: " << typeid(base_).name() << std::endl; // Base_ type: class Base 87 assert(typeid(class Base).name() == typeid(base_).name()); 88 89 std::cout << "pBase_ type: " << typeid(pBase).name() << std::endl; // pBase_ type: class Base * __ptr64 90 assert(typeid(class Base*).name() == typeid(pBase).name()); 91 92 std::cout << "rBase_ type: " << typeid(rBase).name() << std::endl; // Base__ type: class Base 93 assert(typeid(class Base&).name() == typeid(rBase).name()); 94 return 0; 95 96} 97 98void test_typeid2() { 99 100 struct Base { 101 102 }; // non-polymorphic 103 struct Derived : Base { 104 105 }; 106 107 struct Base2 { 108 virtual void foo() {} 109 }; // polymorphic 110 111 struct Derived2 : Base2 { 112 113 }; 114 115 int myint = 50; 116 std::string mystr = "string"; 117 double *mydoubleptr = NULL; 118 119 std::cout << "myint has type: " << typeid(myint).name() << '\n' // myint has type: int 120 << "mystr has type: " << typeid(mystr).name() << '\n' // mystr has type: class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> 121 << "mydoubleptr has type: " << typeid(mydoubleptr).name() << '\n'; // mydoubleptr has type: double * __ptr64 122 123 // std::cout << myint is a glvalue expression of polymorphic type; it is evaluated 124 const std::type_info& r1 = typeid(std::cout); // 50 125 126 std::cout << "#std::cout<<myint has type : " << r1.name() << '\n'; 127 // std::cout<<myint has type: class std::basic_ostream<char, struct std::char_traits<char>> 128 129 const std::type_info& r2 = typeid(std::printf("%d\n", myint)); 130 std::cout << "printf(\"%d\\n\",myint) has type : " << r2.name() << '\n'; // printf(\"%d\\n\",myint) has type : int 131 132 Derived d1; 133 Base& b1 = d1; 134 std::cout << "reference to non-polymorphic base: " << typeid(b1).name() << '\n'; // reference to non-polymorphic base: struct 'int __cdecl test_typeid2(void)'::'2'::Base 135 136 Derived2 d2; 137 Base2& b2 = d2; 138 std::cout << "reference to polymorphic base: " << typeid(b2).name() << '\n'; // reference to polymorphic base: struct 'int __cdecl test_typeid2(void)'::'3'::Derived2 139 140 try { 141 // dereferencing a null pointer: okay for a non-polymoprhic expression 142 std::cout << "mydoubleptr points to " << typeid(*mydoubleptr).name() << '\n'; // mydoubleptr points to double 143 // dereferencing a null pointer: not okay for a polymorphic lvalue 144 Derived2* bad_ptr = NULL; 145 std::cout << "bad_ptr points to..."; // bad_ptr points to... 146 std::cout << typeid(*bad_ptr).name() << '\n'; 147 } 148 catch (const std::bad_typeid& e) { 149 std::cout << " caught " << e.what() << '\n'; // caught Attempted a typeid of NULL pointer! 150 } 151 152} 153 154template < typename T > 155T max(T arg1, T arg2) { 156 std::cout << typeid(T).name() << "s compared." << std::endl; 157 return (arg1 > arg2 ? arg1 : arg2); 158} 159 160int test_typeid3() 161{ 162 class Base { 163 public: 164 virtual void vvfunc() {} 165 }; 166 167 class Derived : public Base {}; 168 169 Derived* pd = new Derived; 170 Base* pb = pd; 171 std::cout << typeid(pb).name() << std::endl; //prints "class Base *" // class 'int __cdecl test_typeid3(void)'::'2'::Base * __ptr64 172 std::cout << typeid(*pb).name() << std::endl; //prints "class Derived" // class 'int __cdecl test_typeid3(void)'::'2'::Derived 173 std::cout << typeid(pd).name() << std::endl; //prints "class Derived *" // class 'int __cdecl test_typeid3(void)'::'2'::Derived * __ptr64 174 std::cout << typeid(*pd).name() << std::endl; //prints "class Derived" // class 'int __cdecl test_typeid3(void)'::'2'::Derived 175 delete pd; 176 177 float a = 1.2, b = 3.4; 178 max(a, b); // floats compared 179 180 max<int>(1,2); 181 182 return 0; 183} 184 185int main() { 186 187 test_typeid1(); 188 cout << "---------------------------------------" << endl; 189 test_typeid2(); 190 test_typeid3(); 191 192 system("pause"); 193 return 0; 194}