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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM