曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 3371|回复: 0
打印 上一主题 下一主题

[RTTI]typeid 运算符和 type_info 结构

[复制链接]

716

主题

734

帖子

2946

积分

超级版主

Rank: 9Rank: 9Rank: 9

积分
2946
跳转到指定楼层
楼主
发表于 2014-1-6 13:56:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
typeid 运算符和 type_info 结构都是 RTTI支持的元素。

typeid 运算符能够确定两个对象是否为同种类型,与 sizeof 有些相似,可以接受两种参数:类名 和 结果为对象的表达式。

typeid 返回一个对 type_info 对象的引用,其中 type_info 是 typeinfo 头文件中定义的一个类。type_info 类还重载了 == 和 != 运算符,以便可以使用这些运算符来对类型进行比较。比如,假设 pg 是一个指向 test 对象的指针,那么下面的表达式的结果为 bool 值 true,否则为 false :
  1. typeid(Test) == typeid (*pg)
复制代码
如果 pg 是一个空指针,程序将引发 bad_typeid 异常;该异常类型在头文件 typeinfo 中声明,它是从 exception 类派生出来的。

type_info 类的实现随厂商而异,但一般都包含一个 name() 成员,该函数返回一个随实现而异的字符串:通常(但不一定)是类的名称。

测试程序:
[C++] 纯文本查看 复制代码
// rtti2.cpp  -- using dynamic_cast, typeid, and type_info
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <typeinfo>
using namespace std;

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 cv = 'A') : Superb(h), ch(cv) {}
    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()
{
    srand(time(0));
    Grand * pg;
    Superb * ps;
    for (int i = 0; i < 5; i++)
    {
        pg = GetOne();
        cout << "Now processing type " << typeid(*pg).name() << ".\n";
        pg->Speak();
        if( ps = dynamic_cast<Superb *>(pg))
            ps->Say();
        if (typeid(Magnificent) == typeid(*pg))
            cout << "Yes, you're really magnificent.\n";
    }
    // std::cin.get();
    return 0;
}

Grand * GetOne()
{
    Grand * p;

    switch( rand() % 3)
    {
        case 0: p = new Grand(rand() % 100);
                    break;
        case 1: p = new Superb(rand() % 100);
                    break;
        case 2: p = new Magnificent(rand() % 100, 'A' + rand() % 26);
                    break;
    }
    return p; 
}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-4-30 11:05 , Processed in 0.076266 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表