typeid 运算符和 type_info 结构都是 RTTI支持的元素。
typeid 运算符能够确定两个对象是否为同种类型,与 sizeof 有些相似,可以接受两种参数:类名 和 结果为对象的表达式。
typeid 返回一个对 type_info 对象的引用,其中 type_info 是 typeinfo 头文件中定义的一个类。type_info 类还重载了 == 和 != 运算符,以便可以使用这些运算符来对类型进行比较。比如,假设 pg 是一个指向 test 对象的指针,那么下面的表达式的结果为 bool 值 true,否则为 false :- 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;
}
|