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 */
  • 相关阅读:
    面试题目——《CC150》链表
    TCP/IP——链路层
    TCP/IP——基本知识
    面试题目——《CC150》数组与字符串
    Java递归算法——三角数字(消除递归)
    Java排序算法——拓扑排序
    Java排序算法——希尔排序
    Python学习笔记——条件和循环
    Python学习笔记——集合类型
    英文写作——冠词的使用(Use 0f Articles)
  • 原文地址:https://www.cnblogs.com/invisible/p/2388674.html
Copyright © 2011-2022 走看看