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 */
  • 相关阅读:
    精通CSS高级Web解决方案(第2版)——读书笔记
    MySQL的知识点总结(一)
    json深拷贝
    Typescript 从Array 继承时碰到的问题
    webpack 笔记
    JavaScript 高效代码
    编写更优雅的 JavaScript 代码
    JavaScript复杂判断优雅写法
    深入浅出Javascript事件循环机制
    关于面向对象封装的思考--持续更新
  • 原文地址:https://www.cnblogs.com/invisible/p/2388674.html
Copyright © 2011-2022 走看看