C++ 中RTTI的使用方法詳解
C++ 中RTTI的使用方法詳解
RTTI是運(yùn)行階段類型識(shí)別(Runtime Type Identification)的簡(jiǎn)稱。這是新添加到c++中的特性之一,很多老式實(shí)現(xiàn)不支持。另一些實(shí)現(xiàn)可能包含開關(guān)RTTI的編譯器設(shè)置。RTTI旨在為程序在運(yùn)行階段確定對(duì)象類型提供一種標(biāo)準(zhǔn)方式。很多類庫已經(jīng)成為其父類對(duì)象提供了實(shí)現(xiàn)這種方式的功能。但由于c++內(nèi)部并不支持,因此各個(gè)廠商的機(jī)制通?;ゲ患嫒?。創(chuàng)建一種RTTI語言標(biāo)準(zhǔn)將使得未來的庫能夠彼此兼容。
c++有3個(gè)支持RTTI的元素
如果可能的話,dynamic_cast 運(yùn)算符將使用一個(gè)指向基類的指針來生成一個(gè)指向派生類的指針;否則,該運(yùn)算符返回0——空指針
typied運(yùn)算符返回一個(gè)指出對(duì)象的類型的值
type_info結(jié)構(gòu)存儲(chǔ)了有關(guān)特定類型的信息
假設(shè)我們有下面的類層次結(jié)構(gòu):
class Grand{ //has virtual methods};
class Super:public Grand {...}
class Magnificent : public Superb{...}
假設(shè)有下面的指針:
Grand *pg = new Grand ; Grand *ps = new Superd; Grand *pm = new Manificent;
1、dynamic_cast 
我們來看一下dynamic_cast的語法,該語法用法如下,其中pg指向一個(gè)對(duì)象 
Superb pm = dynamic_cast< Superb > (pg) ;
這樣 指針 pg 如果可以安全的轉(zhuǎn)換為Superb * 則返回對(duì)象地址,否則返回一個(gè)空指針。 
示例:
// test1002.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include<iostream>
using std::cout;
class Grand
{
private:
  int hold;
public :
  Grand(int h = 0) :hold(h) {}
  virtual void Speak() const { cout << "I am a grand class \n"; }
  virtual int Value() const { return hold; }
};
class Superb :public Grand
{
public :
  Superb(int h = 0) :Grand(h) {}
  void Speak() const { cout << "I am a superb class ! \n"; }
  virtual void Say() const 
  {
    cout << "I hold the superb value of " << Value() << "! \n";
  }
};
class Magnificent : public Superb
{
private :
  char ch;
public :
  Magnificent(int h = 0, char c = 'A') :Superb(h), ch(c)
  {
  }
  void Speak() const 
  {
    cout << "I am a magnificent class !!!! \n";
  }
  void Say() const
  {
    cout << "I hold the character " << ch << " and the integer " << Value() <<"! \n";
  }
};
Grand * GetOne();
int main()
{
  std::srand(static_cast<unsigned int>(std::time(0)));
  Grand * pg;
  Superb * ps;
  for (int i = 0; i < 5; i++)
  {
    pg = GetOne();
    pg->Speak();
    if (ps = dynamic_cast<Superb *>(pg)) {
      ps->Say();
    }
  }
  system("pause");
  return 0;
}
Grand * GetOne()
{
  Grand * p = new Grand();
  switch (std::rand() % 3)
  {
    delete p;
  case 0:p = new Grand(std::rand() % 100); break;
  case 1:p = new Superb(std::rand() % 100); break;
  case 2:p = new Magnificent(std::rand() % 100, std::rand() % 26); break;
  }
  return p;
}
運(yùn)行結(jié)果:
I am a superb class !
I hold the superb value of 3!
I am a magnificent class !!!!
I hold the character  and the integer 5!
I am a grand class
I am a grand class
I am a magnificent class !!!!
I hold the character  and the integer 87!
請(qǐng)按任意鍵繼續(xù). . .
2、typied運(yùn)算符合type_info 類 
typied 運(yùn)算符能夠確定兩個(gè)對(duì)象是否為同類型。他接收兩種參數(shù):1、類名、2、結(jié)果為對(duì)象的表達(dá)式 
typied運(yùn)算符返回的是一個(gè)type_info對(duì)象的引用,type_info在頭文件typeinfo(以前是typeinfo.h)的文件中定義。type_info類重載了== 和!=運(yùn)算符,以便可以使用這些運(yùn)算符來對(duì)類型進(jìn)行比較。 
示例: typeid(Manificnent) == typeid(*pg) 這個(gè)表達(dá)式結(jié)果為 bool值 
如果pg是一個(gè)空指針,程序?qū)⒁l(fā)bad_typied異常。該異常類型是從exception類中派生而來的。是在typeinfo中聲明的。 
type_info類的實(shí)現(xiàn)隨廠商而異,但包含一個(gè)name()成員,該函數(shù)返回一個(gè)隨實(shí)現(xiàn)而異的字符串:通常是類的名字。
示例
// test1002.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include <cstdlib>
#include <ctime>
#include<iostream>
#include <typeinfo>
using std::cout;
class Grand
{
private:
  int hold;
public :
  Grand(int h = 0) :hold(h) {}
  virtual void Speak() const { cout << "I am a grand class \n"; }
  virtual int Value() const { return hold; }
};
class Superb :public Grand
{
public :
  Superb(int h = 0) :Grand(h) {}
  void Speak() const { cout << "I am a superb class ! \n"; }
  virtual void Say() const 
  {
    cout << "I hold the superb value of " << Value() << "! \n";
  }
};
class Magnificent : public Superb
{
private :
  char ch;
public :
  Magnificent(int h = 0, char c = 'A') :Superb(h), ch(c)
  {
  }
  void Speak() const 
  {
    cout << "I am a magnificent class !!!! \n";
  }
  void Say() const
  {
    cout << "I hold the character " << ch << " and the integer " << Value() <<"! \n";
  }
};
Grand * GetOne();
int main()
{
  std::srand(static_cast<unsigned int>(std::time(0)));
  Grand * pg;
  Superb * ps;
  for (int i = 0; i < 5; i++)
  {
    pg = GetOne();
    cout << "Now Process type " << typeid (*pg).name() << ". \n"; //顯示
    pg->Speak();
    if (ps = dynamic_cast<Superb *>(pg)) {
      ps->Say();
    }
  }
  system("pause");
  return 0;
}
Grand * GetOne()
{
  Grand * p = new Grand();
  switch (std::rand() % 3)
  {
    delete p;
  case 0:p = new Grand(std::rand() % 100); break;
  case 1:p = new Superb(std::rand() % 100); break;
  case 2:p = new Magnificent(std::rand() % 100, std::rand() % 26); break;
  }
  return p;
}
運(yùn)行結(jié)果:
Now Process type class Superb.
I am a superb class !
I hold the superb value of 86!
Now Process type class Grand.
I am a grand class
Now Process type class Superb.
I am a superb class !
I hold the superb value of 48!
Now Process type class Grand.
I am a grand class
Now Process type class Magnificent.
I am a magnificent class !!!!
I hold the character and the integer 75!
請(qǐng)按任意鍵繼續(xù). . .
上述代碼添加了一句 typied(*pg).name() 用于輸出類型信息,一般輸出為類名。
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:c語言獲取當(dāng)前工作路徑的實(shí)現(xiàn)代碼(windows/linux)
欄 目:C語言
本文標(biāo)題:C++ 中RTTI的使用方法詳解
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1180.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)數(shù)怎么表達(dá)
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
 - 01-10深入理解C++中常見的關(guān)鍵字含義
 - 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
 - 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
 - 01-10APUE筆記之:進(jìn)程環(huán)境詳解
 - 01-10c++中inline的用法分析
 - 01-10如何尋找數(shù)組中的第二大數(shù)
 


閱讀排行
- 1C語言 while語句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
 - 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
 - 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 04-02jquery與jsp,用jquery
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-10delphi制作wav文件的方法
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10C#中split用法實(shí)例總結(jié)
 


