欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C++ 中RTTI的使用方法詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

C++ 中RTTI的使用方法詳解

RTTI是運行階段類型識別(Runtime Type Identification)的簡稱。這是新添加到c++中的特性之一,很多老式實現(xiàn)不支持。另一些實現(xiàn)可能包含開關RTTI的編譯器設置。RTTI旨在為程序在運行階段確定對象類型提供一種標準方式。很多類庫已經(jīng)成為其父類對象提供了實現(xiàn)這種方式的功能。但由于c++內(nèi)部并不支持,因此各個廠商的機制通?;ゲ患嫒?。創(chuàng)建一種RTTI語言標準將使得未來的庫能夠彼此兼容。

c++有3個支持RTTI的元素

如果可能的話,dynamic_cast 運算符將使用一個指向基類的指針來生成一個指向派生類的指針;否則,該運算符返回0——空指針

typied運算符返回一個指出對象的類型的值

type_info結構存儲了有關特定類型的信息

假設我們有下面的類層次結構:

class Grand{ //has virtual methods};
class Super:public Grand {...}
class Magnificent : public Superb{...}

假設有下面的指針:

Grand *pg = new Grand ;
Grand *ps = new Superd;
Grand *pm = new Manificent;

1、dynamic_cast

我們來看一下dynamic_cast的語法,該語法用法如下,其中pg指向一個對象

Superb pm = dynamic_cast< Superb > (pg) ; 

這樣 指針 pg 如果可以安全的轉換為Superb * 則返回對象地址,否則返回一個空指針。

示例:

// test1002.cpp : 定義控制臺應用程序的入口點。
//

#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;
}
運行結果:
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!
請按任意鍵繼續(xù). . .

2、typied運算符合type_info 類

typied 運算符能夠確定兩個對象是否為同類型。他接收兩種參數(shù):1、類名、2、結果為對象的表達式

typied運算符返回的是一個type_info對象的引用,type_info在頭文件typeinfo(以前是typeinfo.h)的文件中定義。type_info類重載了== 和!=運算符,以便可以使用這些運算符來對類型進行比較。

示例: typeid(Manificnent) == typeid(*pg) 這個表達式結果為 bool值

如果pg是一個空指針,程序將引發(fā)bad_typied異常。該異常類型是從exception類中派生而來的。是在typeinfo中聲明的。

type_info類的實現(xiàn)隨廠商而異,但包含一個name()成員,該函數(shù)返回一個隨實現(xiàn)而異的字符串:通常是類的名字。

示例

// test1002.cpp : 定義控制臺應用程序的入口點。
//

#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;
}
運行結果:
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!
請按任意鍵繼續(xù). . .

上述代碼添加了一句 typied(*pg).name() 用于輸出類型信息,一般輸出為類名。

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

上一篇:c語言獲取當前工作路徑的實現(xiàn)代碼(windows/linux)

欄    目:C語言

下一篇:C/C++中的名字空間與作用域示例詳解

本文標題:C++ 中RTTI的使用方法詳解

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1180.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有