一,什么是动态类型
基类指针所指对象的实际类型
基类指针是否可以强制类型转换为子类指针取决于动态类型!
二,方法
1,利用多态
- 必须从基类开始提供类型虚函数
- 所有的派生类都必须重写类型虚函数
- 每个派生类的类型ID必须唯一
2,利用dynamic_cast
dynamic_cast这个关键字如果要转换的实际类型和指定的类型不一样,则会返回NULL。例如当指定类型为子类对象时,如果父类指针的动态类型是这个子类对象时,没有错误,而动态类型是父类对象或者其他子类对象时 错误
- dynamic_cast要求使用的目标对象类型是多态的,即:所在类族至少有一个虚函数
- 只能用于指针和引用之间
- 用于指针转换时,转换失败返回空指针
- 用于引用转换时,转换失败将引发bad_cast异常
3,利用typeid(推荐这种方法)
typdeid 专门用于动态类型识别
- 它是一个关键字
- typeid返回一个type_info类对象
- 使用时需要包含头文件<typeinfo>
- 直接指定对象或者类型,普通类型普通对象也可以
三,使用例子
a,多态
class Parent
{
public:
enum { ID = 0 };
virtual int type()
{
return ID;
}
};
class Child : public Parent
{
public:
enum { ID = 1 };
int type()
{
return ID;
}
int add(int a, int b)
{
return a + b;
}
};
void test(Parent* p)
{
if( p->type() == Child::ID )
{
Child* c = (Child*)p;
cout<<"Dynamic Type: "<<"Child"<<endl;
cout<<"add: "<<c->add(2, 3)<<endl;
}
if( p->type() == Parent::ID )
{
cout<<"Dynamic Type: "<<"Parent"<<endl;
}
}
int main(int argc, char *argv[])
{
Parent parent;
Child child;
test(&parent);
test(&child);
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
b,dynamic_cast
#include <cstdlib>
#include <iostream>
using namespace std;
class Parent
{
public:
virtual ~Parent()
{
}
};
class Child : public Parent
{
public:
int add(int a, int b)
{
return a + b;
}
};
class NewChild : public Parent
{
};
void test(Parent* p)
{
Child* c = dynamic_cast<Child*>(p);//p的动态类型是 非Child对象的话会返回NULL
if( c != NULL )
{
cout<<"Dynamic Type: "<<"Child"<<endl;
cout<<"add: "<<c->add(2, 3)<<endl;
}
else
{
if( dynamic_cast<NewChild*>(p) != NULL )
{
cout<<"Dynamic Type: "<<"NewChild"<<endl;
}
else
{
cout<<"Dynamic Type: "<<"Parent"<<endl;
}
}
}
int main(int argc, char *argv[])
{
Parent parent;
Child child;
NewChild nc;
test(&parent);
test(&child);
test(&nc);
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}
c,typeid
class Parent
{
public:
virtual ~Parent()
{
}
};
class Child : public Parent
{
public:
int add(int a, int b)
{
return a + b;
}
};
class NewChild : public Parent
{
};
void test(Parent* p)
{
if( typeid(*p) == typeid(Child) )
{
Child* c = dynamic_cast<Child*>(p);
cout<<"Dynamic Type: "<<"Child"<<endl;
cout<<"add: "<<c->add(2, 3)<<endl;
}
else if( typeid(*p) == typeid(NewChild) )
{
cout<<"Dynamic Type: "<<"NewChild"<<endl;
}
else if( typeid(*p) == typeid(Parent) )
{
cout<<"Dynamic Type: "<<"Parent"<<endl;
}
}
int main(int argc, char *argv[])
{
Parent parent;
Child child;
NewChild nc;
int index;
char ch;
const type_info& tp = typeid(parent);
const type_info& tc = typeid(child);
const type_info& tn = typeid(nc);
const type_info& ti = typeid(index);
const type_info& tch = typeid(ch);
cout<<tp.name()<<endl;
cout<<tc.name()<<endl;
cout<<tn.name()<<endl;
cout<<ti.name()<<endl;
cout<<tch.name()<<endl;
test(&parent);
test(&child);
test(&nc);
cout << "Press the enter key to continue ...";
cin.get();
return EXIT_SUCCESS;
}