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