在入門C++過程中,我們經常會遇到無法判斷對象類型的情況。
頭文件( VS編譯器 )
#include <typeinfo>
typeid(對象).name();
例子:
const int a = 10, &b = a; auto e = a; auto d = b; MyClass c; MyStruct s; cout << typeid(a).name() << endl; // int cout << typeid(&b).name() << endl;// int const * cout << typeid(e).name() << endl; // int cout << typeid(d).name() << endl; // int cout << typeid(c).name() << endl; // class MyClass cout << typeid(s).name() << endl; // Struct MyStruct