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\n";
 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\n";
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\n";
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\n";
 9     else cout<<"this is son2\n";
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\n";
14     }
15     else if(strcmp(sonstr[1], typeid(*pf).name()) == 0)
16     {
17         cout<<"this is son2\n";
18     }
19 
20     pf = &s2;
21     if(strcmp(sonstr[0], typeid(*pf).name()) == 0)
22     {
23         cout<<"this is son1\n";
24     }
25     else if(strcmp(sonstr[1], typeid(*pf).name()) == 0)
26     {
27         cout<<"this is son2\n";
28     }
29     return 0;
30 }

 

 【版權聲明】轉載請注明出處 http://www.cnblogs.com/TenosDoIt/p/3176525.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM