C++技能 runtime type identification(RTTI)
运行时类型识别
在使用多态的时候经常用到。本文将会介绍RTTI的几个特征。
1. 运行时类型转换
下面的程序模仿了dynamic_cast<type_id>()类型转化符号,根据每个类的id来判断当前的类型,如果id不匹配,则调用dynacast函数会转化失败
#include <iostream>
#include<vector>
using namespace std;
//base class
class Security{
protected:
enum{BASEID = 0};
public:
virtual ~Security(){}
virtual bool isA(int id){
return id == BASEID;
}
};
///继承基类
class Stock : public Security{
typedef Security Super;
protected:
enum{ OFFSET = 1, TYPEID = BASEID + OFFSET};
public:
bool isA(int id){
return id == TYPEID || Super::isA(id);
}
//类型转换--通过检查id来判断,基类的id = 0,stock id = 1,如果当前id = stock id,则允许向下转换,否则返回NULL
static Stock* dynacast(Security* s){
return (s->isA(TYPEID)) ? static_cast<Stock*>(s) : 0;
}
};
//继承了基类
class Bond : public Security{
typedef Security Super;
protected:
enum{ OFFSET = 2, TYPEID = BASEID + OFFSET};
public:
bool isA(int id){
return id == TYPEID || Super::isA(id);
}
static Bond* dynacast(Security* s){
return (s->isA(TYPEID)) ? static_cast<Bond*>(s) : 0;
}
};
//继承了基类
class Investment : public Security{
typedef Security Super;
protected:
enum{ OFFSET = 3, TYPEID = BASEID + OFFSET};
public:
bool isA(int id){
return id == TYPEID || Super::isA(id);
}
static Investment* dynacast(Security* s){
return (s->isA(TYPEID)) ? static_cast<Investment*>(s) : 0;
}
void special(){
cout << "special Investment function\n";
}
};
//基类的孙子类
class Metal : public Investment{
typedef Security Super;
protected:
enum{ OFFSET = 4, TYPEID = BASEID + OFFSET};
public:
bool isA(int id){
return id == TYPEID || Super::isA(id);
}
static Metal* dynacast(Security* s){
return (s->isA(TYPEID)) ? static_cast<Metal*>(s) : 0;
}
};
int main(){
vector<Security*> portfolio;
//基类指向派生类们
portfolio.push_back(new Metal);
portfolio.push_back(new Investment);
portfolio.push_back(new Bond);
portfolio.push_back(new Stock);
//
for(vector<Security*>::iterator it = portfolio.begin(); it != portfolio.end(); it++){
Investment* cm = Investment::dynacast(*it);//指针转化
if(cm)
cm->special();
else
cout << "not an Investment\n";
}
Security* sp = new Metal;
Investment* cp = Investment::dynacast(sp);//根据多态理论调用对于的dynacast ,这里调用metal类的isA
if(cp) cout << "it's a Investment\n";
Metal* mp = Metal::dynacast(sp);
//转化成功
if(mp) cout << "it's a metal\n";
//释放内存
for(vector<Security*>::iterator it = portfolio.begin(); it != portfolio.end(); it++){
delete *it;
}
return 0;
}
上面是指针的转化,如果使用dynamic_cast程序会简短很多,
#include <iostream>
#include<vector>
using namespace std;
//base class
class Security{
public:
virtual ~Security(){}
};
///继承基类
class Stock : public Security{
};
//继承了基类
class Bond : public Security{
};
//继承了基类
class Investment : public Security{
public:
void special(){
cout << "special Investment function\n";
}
};
//基类的孙子类
class Metal : public Investment{
};
int main(){
vector<Security*> portfolio;
//基类指向派生类们
portfolio.push_back(new Metal);
portfolio.push_back(new Investment);
portfolio.push_back(new Bond);
portfolio.push_back(new Stock);
//
for(vector<Security*>::iterator it = portfolio.begin(); it != portfolio.end(); it++){
Investment* cm = dynamic_cast<Investment*>(*it);//指针转化
if(cm)
cm->special();
else
cout << "not an Investment\n";
}
Security* sp = new Metal;
//转化成功
Investment* cp = dynamic_cast<Investment*>(sp);
if(cp) cout << "it's a Investment\n";
Metal* mp = dynamic_cast<Metal*>(sp);
//转化成功
if(mp) cout << "it's a Metal\n";
//释放内存
for(vector<Security*>::iterator it = portfolio.begin(); it != portfolio.end(); it++){
delete *it;
}
return 0;
}
dynamic_cast要求多态,还好这里的基类的析构是虚函数,因此可以使用 dynamic_cast。另外dynamic_cast只能做指针或者引用的转化
如果是普通的类型转化,则无法用是否为空指针来判断,这时可以用异常处理,如果无法转化,dynamic_cast会抛出异常。
2.typedid操作符
typeid可以获取对象运行时的信息,他会返回一个type_info对象,该对象记录了和这个对象有关的应用信息,比如:
这个对象是多态的,则它将会给出那个应用的大部分派生类信息;否则就给出静态信息,typeid操作符的一个用途是获得一个对象的动态类型的名称
输出结果和编译器有关,有的直接输出名字,有的输出pk什么的,p代表指针,k代表const修饰符
#include <iostream>
#include<vector>
#include<typeinfo>
using namespace std;
struct PolyBase{
virtual ~PolyBase(){
}
};
struct PolyDer : public PolyBase{
PolyDer(){}
};
struct NonPolyBase{
};
struct NonPolyDer : public NonPolyBase{
NonPolyDer(int){ }
};
int main(){
const PolyDer pd;
const PolyBase* ppd = &pd;//父类指向子类
cout << typeid(ppd).name() << endl;//输出父类自己的名字
cout << typeid(*ppd).name() << endl; //输出子类名称
cout << boolalpha << (typeid(*ppd) == typeid(pd)) << endl;//输出true
const NonPolyDer npd(1);
const NonPolyBase* nppd = &npd;
cout << typeid(nppd).name() << endl;//输出父类
cout << typeid(*nppd).name() << endl; //输出父类
cout << boolalpha << (typeid(*nppd) == typeid(npd)) << endl;//false
int i;
cout << typeid(i).name() << endl;
return 0;
}
对于第一种含有虚函数,和第二种不含有虚函数是完全不同的。因为typeid对多态敏感。
使用指针的时候,输出的是指针的静态类型,当调用对象解析的时候,则会输出动态类型
而对于不含虚函数的类,则不会有变化,两次输出都是父类的名字,typeid也支持内置的类型
typeid不支持赋值操作,也没用可供访问的构造函数
3.继承体系的中间层次的转化
比如有这么一个继承体系
class B1{virtual ~B1(){}};
class B2{virtual ~B2(){}};
class MI : public B1, public B2{};
class Mi2 : public MI{};
那么创建一个Mi2对象,可以转化为MI,B1,B2;
#include <iostream>
using namespace std;
class B1{
public:virtual ~B1(){}
};
class B2{
public:virtual ~B2(){}
};
class MI : public B1, public B2{};
class Mi2 : public MI{};
int main(){
B2* b2 = new Mi2;
Mi2* pmi2 = dynamic_cast<Mi2*>(b2);
B1* b1 = dynamic_cast<B1*>(b2);
MI* mi = dynamic_cast<MI*>(b2);
return 0;
}
4 void型指针
不可以把void*和typeid和dynamic_cast联系起来
5.虚基类类型向下转化
当基类是虚基类的时候,c++不允许C语言的默认指针转化,但是可以使用dynamic_cast;