zoukankan      html  css  js  c++  java
  • c++ 动态判断基类指针指向的子类类型(typeid)

    我们在程序中定义了一个基类,该基类有n个子类,为了方便,我们经常定义一个基类的指针数组,数组中的每一项指向都指向一个子类,那么在程序中我们如何判断这些基类指针是指向哪个子类呢?

    本文提供了两种方法 (1) 自定义类id, (2)typeid

    一、自定义id

    如下所示基类father有两个子类son1 和 son2,我们在基类中定义类虚函数id,子类中分别重载了该函数,各个子类返回值都不同

     1 class father
     2 {
     3 public:
     4     virtual void fun()
     5     {
     6         cout<<"this is father fun call
    ";
     7     }
     8     virtual int id()
     9     {
    10         return 0;
    11     }
    12 };
    13 
    14 class son1: public father
    15 {
    16 public:
    17 
    18     void fun()
    19     {
    20         cout<<"this is the son1 fun call
    ";
    21     }
    22 
    23     int id()
    24     {
    25         return 1;
    26     }
    27 
    28 };
    29 
    30 class son2: public father
    31 {
    32 public:
    33 
    34     void fun()
    35     {
    36         cout<<"this is the son2 fun call
    ";
    37     }
    38 
    39     int id()
    40     {
    41         return 2;
    42     }
    43 };

    通过如下方法我们可以在程序中动态的判断基类指针指向的子类类型

     1 int main()
     2 {
     3     father * pf;
     4     son1 s1;
     5     son2 s2;
     6     pf = &s1;
     7     if(pf->id() == 1)
     8         cout<<"this is son1
    ";
     9     else cout<<"this is son2
    ";
    10 }

    二、typeid

    typeid是c++的关键字,typeid操作符的返回结果是名为type_info的标准库类型的对象的引用(在头文件typeinfo中定义

    ISO C++标准并没有确切定义type_info,它的确切定义编译器相关的,但是标准却规定了其实现必需提供如下四种操作:

    type_info类提供了public虚 析构函数,以使用户能够用其作为基类。它的默认构造函数和拷贝构造函数及赋值操作符都定义为private,所以不能定义或复制type_info类型的对象。

    程序中创建type_info对象的唯一方法是使用typeid操作符(由此可见,如果把typeid看作函数的话,其应该是type_info的 友元)

    type_info的name成员函数返回C-style的字符串,用来表示相应的类型名,但务必注意这个返回的类型名与程序中使用的相应类型名并不一定一致,这具体由编译器的实现所决定的,标准只要求实现为每个类型返回唯一的字符串

    typeid 的参数可以使指针,可以使对象,可以是普通变量等。

    具体判断基类指针指向的类型方法如下(类的定义同上):

     1 int main()
     2 {
     3     char sonstr[2][100];
     4     //由于不知道编译器对typeid.name返回的字符串,因此预先保存好返回的字符串
     5     strcpy(sonstr[0], typeid(son1).name());
     6     strcpy(sonstr[1], typeid(son2).name());
     7     father * pf;
     8     son1 s1;
     9     son2 s2;
    10     pf = &s1;
    11     if(strcmp(sonstr[0], typeid(*pf).name()) == 0)
    12     {
    13         cout<<"this is son1
    ";
    14     }
    15     else if(strcmp(sonstr[1], typeid(*pf).name()) == 0)
    16     {
    17         cout<<"this is son2
    ";
    18     }
    19 
    20     pf = &s2;
    21     if(strcmp(sonstr[0], typeid(*pf).name()) == 0)
    22     {
    23         cout<<"this is son1
    ";
    24     }
    25     else if(strcmp(sonstr[1], typeid(*pf).name()) == 0)
    26     {
    27         cout<<"this is son2
    ";
    28     }
    29     return 0;
    30 }

     【版权声明】转载请注明出处 http://www.cnblogs.com/TenosDoIt/p/3176525.html

  • 相关阅读:
    经典小程序源码及其下载地址
    关闭ios虚拟键盘的几种方法
    从零开始学ios开发(十八):Storyboards(下)
    iOS应用程序生命周期(前后台切换,应用的各种状态)详解
    从零开始学ios开发(十九):Application Settings and User Defaults(上)
    关于IOS中的delegate必须知道的事情
    ios多线程和进程的区别(转载)
    ios各种手势,很有意思
    IOS中GPS定位偏移纠正(适用于Google地图)
    [iOS]深入浅出 iOS 之多线程 NSThread
  • 原文地址:https://www.cnblogs.com/TenosDoIt/p/3176525.html
Copyright © 2011-2022 走看看