C++ 获取对象类型

在入门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