动态类型
由于赋值兼容性原则,基类指针可以直接指向派生类对象,因此可能存在指针所指类型与具体指向的对象类型不同的情况。
如下图所示:
动态类型:指的是基类指针所指向的对象的实际类型。
这样就会存在如下的情况:
基类指针是否可以强制类型转换为子类指针取决于动态类型!因此就需要对动态类型进行识别。
动态类型识别—方法一
采用多态的方法,在每个类中定义返回类的类型的虚函数。
C++中的多态根据实际的对象类型调用对应的虚函数。
可以在基类中定义虚函数返回具体的类型信息。
所有的派生类都必须实现类型相关的虚函数。
每个类中的类型虚函数都需要不同的实现。
代码举例:
#include <cstdlib>
#include <iostream>
using namespace std;
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;
}
缺陷:
必须从基类开始提供类型虚函数。
所有的派生类都必须重写类型虚函数。
每个派生类的类型ID必须唯一。
利用虚函数进行动态类型识别的方法可以满足工程的需要,但是维护性会随着派生类的增多而成指数级增加。不同的工程师可能对ID的定义也不同。
动态类型识别—方法二
新的关键字dynamic_cast
dynamic_cast是C++中的新型转换关键字。
dynamic_cast主要用于基类和派生类之间的转换。
dynamic_cast要求使用的目标对象类型是多态的。
即:所在类族至少有一个虚函数。
用于指针转换时,转换失败返回空指针。
用于引用转换时,转换失败将引发bad_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);//动态类型转换
if( c != NULL )//如果转换成功
{
cout<<"Dynamic Type: "<<"Child"<<endl;
cout<<"add: "<<c->add(2, 3)<<endl;
}
else//如果转换为child失败
{
if( dynamic_cast<NewChild*>(p) != NULL )//如果转换为new child成功
{
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;
}
dynamic_cast的优势:
不用显示的声明和定义类型虚函数。
不用为类族中的每个类分配类型ID。
dynamic_cast的缺陷:
只能用于具有虚函数的类族。
使用dynamic_cast进行动态类型识别可以取代类型虚函数的方案,但是在本质上dynamic _cast还是需要类族中存在虚函数(在工程上常把析构函数作为这个虚函数定义)。
由于每个类都需要析构函数,因此为了满足上面的虚函数要求,一般在工程中,把类的析构函数定义为虚函数。
动态类型识别—方法三
C++提供了typeid关键字用于动态获取类型信息。
typeid关键字返回对应参数的类型信息。
typeid返回一个type_info类对象。
当typeid的参数为NULL时,抛出bad_typeid异常。
type_info类的使用需要包含< typeinfo > 头文件。
type_info类如下图所示:
typeid是C++的关键字,并不是函数,因此可以使用数据类型作为参数,这一点与sizeof关键字相同。
typeid关键字使用,代码举例:
#include <cstdlib>
#include <iostream>
#include <typeinfo> //判断动态类型
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)//比较类型 ,进行识别
{
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;
}
小结
C++中可以通过多态的方式进行动态类型识别,但是要求每个类定义虚函数,并且要返回类的类型。
dynamic_cast关键字是可用于动态类型识别,但是要求定义虚函数。
typeid关键字在C++中专用于动态类型的识别。
在这三种方法中,没有优劣之分,typeid关键字的方法在早期版本的编译器中可能无法使用,那样只能采用多态的方法进行动态类型识别,使用typeid关键字也可能导致程序可移植性下降。