zoukankan      html  css  js  c++  java
  • the use of typeid

    C++语言: Codee#25754
    01 // type_info example
    02 #include <iostream>
    03 #include <typeinfo>
    04 using namespace std;
    05
    06 struct Base {};
    07 struct Derived : Base {};
    08 struct Poly_Base
    09 {
    10     virtual void Member() {}
    11 };
    12 struct Poly_Derived: Poly_Base {};
    13
    14 int main()
    15 {
    16     // built-in types:
    17     int i;
    18     int * pi;
    19     cout << "int is: " << typeid(int).name() << endl;
    20     cout << "  i is: " << typeid(i).name() << endl;
    21     cout << " pi is: " << typeid(pi).name() << endl;
    22     cout << "*pi is: " << typeid(*pi).name() << endl << endl;
    23
    24     // non-polymorphic types:
    25     Derived derived;
    26     Base* pbase = &derived;
    27     cout << "derived is: " << typeid(derived).name() << endl;
    28     cout << " *pbase is: " << typeid(*pbase).name() << endl;
    29     cout << boolalpha << "same type? ";
    30     cout << ( typeid(derived) == typeid(*pbase) ) << endl << endl;
    31
    32     // polymorphic types:
    33     Poly_Derived polyderived;
    34     Poly_Base* ppolybase = &polyderived;
    35     cout << "polyderived is: " << typeid(polyderived).name() << endl;
    36     cout << " *ppolybase is: " << typeid(*ppolybase).name() << endl;
    37     cout << boolalpha << "same type? ";
    38     cout << ( typeid(polyderived) == typeid(*ppolybase) ) << endl << endl;
    39 }
    40 /*
    41 int is: i
    42   i is: i
    43 pi is: Pi
    44 *pi is: i
    45
    46 derived is: 7Derived
    47 *pbase is: 4Base
    48 same type? false
    49
    50 polyderived is: 12Poly_Derived
    51 *ppolybase is: 12Poly_Derived
    52 same type? true
    53
    54
    55 Process returned 0 (0x0)   execution time : 0.041 s
    56 Press any key to continue.
    57
    58
    59 */
  • 相关阅读:
    团队博客创建
    筼筜湖美景
    作业11-网络
    作业11-多线程
    多线程-冲突与同步代码
    Java第12次实验提纲(JSP简单入门)
    作业-JSP简单入门
    Java Web参考资料
    面向对象设计大作业迭代任务
    在码云(gitee)上展开程序类课程教学
  • 原文地址:https://www.cnblogs.com/invisible/p/2388674.html
Copyright © 2011-2022 走看看