/*
名称:C++ 运行时类型识别
作者:Michael Joessy
日期:2017-06-06
知识:Run-Time Type Information
通过运行时类型信息程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型。
typeid dynamic_cast
注意:
dynamic_cast:
只能用于指针和引用的转换;
要转换的类型中必须包含虚函数;
转换成功返回子类的地址,失败返回NULL.
typeid:
typeid返回一个type_info(#include <typeinfo>)对象的引用;
如果想通过基类的指针获得派生类的数据类型,基类必须带有虚函数;
只能获取对象的实际类型.
*/
/*
class type_info {
public:
virtual ~type_info();
_CRTIMP_PURE bool __CLR_OR_THIS_CALL operator==(const type_info& rhs) const;
_CRTIMP_PURE bool __CLR_OR_THIS_CALL operator!=(const type_info& rhs) const;
_CRTIMP_PURE int __CLR_OR_THIS_CALL before(const type_info& rhs) const;
_CRTIMP_PURE const char* __CLR_OR_THIS_CALL name(__type_info_node* __ptype_info_node = &__type_info_root_node) const;
_CRTIMP_PURE const char* __CLR_OR_THIS_CALL raw_name() const;
private:
void *_m_data;
char _m_d_name[1];
__CLR_OR_THIS_CALL type_info(const type_info& rhs);
type_info& __CLR_OR_THIS_CALL operator=(const type_info& rhs);
_CRTIMP_PURE static const char *__CLRCALL_OR_CDECL _Name_base(const type_info *,__type_info_node* __ptype_info_node);
_CRTIMP_PURE static void __CLRCALL_OR_CDECL _Type_info_dtor(type_info *);
};
*/
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
class Flyable
{
public:
; //起飞
; //着陆
protected:
private:
};
class Bird : public Flyable
{
public:
void forageing() //觅食
{
cout << "Bird->forageing()" << endl;
}
virtual void takeoff() //起飞
{
cout << "Bird->takeoff()" << endl;
}
virtual void land() //着陆
{
cout << "Bird->land()" << endl;
}
protected:
private:
};
class Plane : public Flyable
{
public:
void carry() //运输
{
cout << "Plane->carry()" << endl;
}
virtual void takeoff() //起飞
{
cout << "Plane->takeoff()" << endl;
}
virtual void land() //着陆
{
cout << "Plane->land()" << endl;
}
protected:
private:
};
void doSomething(Flyable* pObj)
{
cout << typeid(*pObj).name() << endl;
pObj->takeoff();
if (typeid(*pObj) == typeid(Bird))
{
Bird* pBird = dynamic_cast<Bird*>(pObj);
if (pBird)
{
pBird->forageing();
}
}
if (typeid(*pObj) == typeid(Plane))
{
Plane* pPlane = dynamic_cast<Plane*>(pObj);
if (pPlane)
{
pPlane->carry();
}
}
pObj->land();
}
int main(void)
{
Flyable* pObj1 = new Bird;
Flyable* pObj2 = new Plane;
doSomething(pObj1);
doSomething(pObj2);
/************************************************************************/
/* typeid usage
/************************************************************************/
;
cout << typeid(nNumber).name() << endl;
Flyable* p = new Bird;
cout << typeid(p).name() << endl;
cout << typeid(*p).name() << endl;
cin.get();
;
}