最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年09月09日 10:01:27   投稿:lqh  
這篇文章主要介紹了C++ 中RTTI的使用方法詳解的相關(guān)資料,希望通過本文大家能理解使用RTTI,需要的朋友可以參考下

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

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

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

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

typied運(yùn)算符返回一個指出對象的類型的值

type_info結(jié)構(gòu)存儲了有關(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指向一個對象

Superb pm = dynamic_cast< Superb > (pg) ; 

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

示例:

// test1002.cpp : 定義控制臺應(yīng)用程序的入口點。
//

#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!
請按任意鍵繼續(xù). . .

2、typied運(yùn)算符合type_info 類

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

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

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

如果pg是一個空指針,程序?qū)⒁l(fā)bad_typied異常。該異常類型是從exception類中派生而來的。是在typeinfo中聲明的。

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

示例

// test1002.cpp : 定義控制臺應(yīng)用程序的入口點。
//

#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!
請按任意鍵繼續(xù). . .

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

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

相關(guān)文章

  • C語言入門篇--初識C語言及數(shù)據(jù)類型

    C語言入門篇--初識C語言及數(shù)據(jù)類型

    本篇文章是c語言基礎(chǔ)篇,主要為大家介紹了C語言的基本類型,為大家介紹了什么是C語言,希望可以幫助大家快速入門c語言的世界,更好的理解c語言
    2021-08-08
  • C語言模擬實現(xiàn)密碼輸入的示例代碼

    C語言模擬實現(xiàn)密碼輸入的示例代碼

    本文主要介紹了C語言模擬實現(xiàn)密碼輸入的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • C++中的四種類型轉(zhuǎn)換

    C++中的四種類型轉(zhuǎn)換

    類型轉(zhuǎn)換有c風(fēng)格的,當(dāng)然還有c++風(fēng)格的。c風(fēng)格的轉(zhuǎn)換的格式很簡單(TYPE)EXPRESSION,但是c風(fēng)格的類型轉(zhuǎn)換有不少的缺點,有的時候用c風(fēng)格的轉(zhuǎn)換是不合適的,因為它可以在任意類型之間轉(zhuǎn)換,
    2015-08-08
  • c語言求1+2+...+n的解決方法

    c語言求1+2+...+n的解決方法

    本篇文章是對在c語言中求1+2+...+n的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C&C++設(shè)計風(fēng)格選擇 命名規(guī)范

    C&C++設(shè)計風(fēng)格選擇 命名規(guī)范

    本文難免帶有主觀選擇傾向,但是會盡量保持客觀的態(tài)度歸納幾種主流的命名風(fēng)格,僅供參考
    2018-04-04
  • C++語言pow函數(shù)的具體使用

    C++語言pow函數(shù)的具體使用

    本文主要介紹了C++語言pow函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Qt實現(xiàn)編輯框失去焦點隱藏功能

    Qt實現(xiàn)編輯框失去焦點隱藏功能

    這篇文章主要為大家詳細(xì)介紹了Qt實現(xiàn)的一個簡單的編輯框操作——主窗口失去焦點隱藏功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-10-10
  • 一篇文章帶你了解C++面向?qū)ο缶幊?-繼承

    一篇文章帶你了解C++面向?qū)ο缶幊?-繼承

    這篇文章主要介紹了解析C++面對象編程--繼承的運(yùn)用,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-08-08
  • C語言科學(xué)計算入門之矩陣乘法的相關(guān)計算

    C語言科學(xué)計算入門之矩陣乘法的相關(guān)計算

    這篇文章主要介紹了C語言科學(xué)計算入門之矩陣乘法的相關(guān)計算,文章中還介紹了矩陣相關(guān)的斯特拉森算法的實現(xiàn),需要的朋友可以參考下
    2015-12-12
  • C++ vector模擬實現(xiàn)的代碼詳解

    C++ vector模擬實現(xiàn)的代碼詳解

    vector是表示可變大小數(shù)組的序列容器,就像數(shù)組一樣,vector也采用的連續(xù)存儲空間來存儲元素,本質(zhì)講,vector使用動態(tài)分配數(shù)組來存儲它的元素,本文將給大家詳細(xì)介紹一下C++ vector模擬實現(xiàn),需要的朋友可以參考下
    2023-07-07

最新評論

新邵县| 永平县| 龙里县| 泰安市| 临潭县| 文成县| 三明市| 莆田市| 嘉荫县| 阳泉市| 定边县| 小金县| 玉溪市| 平罗县| 得荣县| 灵宝市| 云浮市| 大埔县| 郓城县| 监利县| 武夷山市| 兴安盟| 花莲市| 小金县| 扎赉特旗| 固原市| 阿尔山市| 博白县| 苗栗县| 丁青县| 静乐县| 武穴市| 平利县| 河津市| 河源市| 永胜县| 太原市| 靖宇县| 宁海县| 德兴市| 云霄县|