简介:
C++中提供了动态强制(dynamic_cast),是一种运行时类型识别机制,可以从一个虚的基类强制到一个派生类。我们常常需要知道,在运行过程中,我们真正在使用的是哪个类的对象,这时,可以明确地使用dynamic_cast来得到运行时类型的信息。
数据类型转换系列文章:
dynamic_cast注意事项以及示例代码:
注意事项:
虚基类(或称抽象类)可以使用dynamic_cast,但是,非虚基类不可以。
在dynamic_cast被设计之前,C++无法实现从一个虚基类到派生类的强制转换。dynamic_cast就是为解决虚基类到派生类的转换而设计的。
示例代码:
示例代码1: 子类--->父类(虚基类)
#include <stdio.h>
#include <stdlib.h>
//美术家
class Artist {
public:
virtual void draw() {printf("Artist draw\n");}
};
//音乐家
class Musician {
public:
};
//教师
class Teacher {
public:
virtual void teachStu() {printf("Teacher teachStu\n");}
};
//即是美术,又是音乐家,又是教师的人
class People: public virtual Artist,public virtual Musician,public Teacher {
public:
};
void test1() {
People *p1 = new People();
p1->draw();
p1->teachStu();
printf("\ndynamic_cast test:\n");
Artist *a1 = dynamic_cast<Artist*>(p1);
//Artist *a1 = p1; //向上转换,C++总是能够正确识别。即将派生类的指针赋值给基类指针。
a1->draw(); //success: 打印Artist draw
//a1->teachStu(); //error: no member named 'teachStu' in 'Artist'
}
int main() {
test1();
return 0;
}
说明:
1)继承关系:People 继承自Artist,Musician,Teacher三个类;
2)语意:Artist美术家,Musician音乐家,Teacher教师。
People类:定义一个特殊的People类,它的特点是:既是美术家,又是音乐家,也是教师。
上面的代码,运行结果如下:
Artist draw
Teacher teachStu
dynamic_cast test:
Artist draw
分析:
下面两条语句均正确,都能由子类People转化为父类Artist,并且调用父类Artist的成员函数成功。
Artist *a1 = dynamic_cast<Artist*>(p1); 等价于
Artist *a1 = p1;
a1->draw(); //success: 打印people teachStu
可见,向上转换,无论是否用dynamic_cast,C++总是能够正确识别,即将派生类的指针赋值给基类指针。
示例代码2: 父类(虚基类)--->子类,采用虚继承的方式:
对于上面的类,再看如下代码:
void test1() {
People *p1 = new People();
printf("\ndynamic_cast test:\n");
Artist *a1 = p1; //success
//People *p2 = (People*)a1; //error: cannot cast 'Artist *' to 'People *' via virtual base 'Artist'
People *p3 = dynamic_cast<People*>(a1);//success:加了dynamic_cast,进行强转
}
分析:
1) //People *p2 = (People*)a1; //error: cannot cast 'Artist *' to 'People *' via virtual base 'Artist',
明确说明,无法直接用(T*)b的形式去进行由父类指针到派生类指针的转换;
2) People *p3 = dynamic_cast<People*>(a1);//success:加了dynamic_cast,进行强转。
每个类都会在内存中保存一份类型信息,编译器会将存在继承关系的类的类型信息使用指针“连接”起来,从而形成一个继承链(Inheritance Chain)。
dynamic_cast使用注意事项:
1)查找规则:当使用 dynamic_cast 对指针进行类型转换时,会先找到该指针指向的对象,再根据对象找到当前类(指针指向的对象所属的类)的类型信息,并从此节点开始沿着继承链向上遍历(注意是向上),如果找到了要转化的目标类型,那么说明这种转换是安全的,就能够转换成功,如果没有找到要转换的目标类型,那么说明这种转换存在较大的风险,就不能转换。
2)作用对象:注意dynamic_cast转换符只能用于含有虚函数的类。
现在,用RTTI来解释test1()函数中的代码:
p1是派生类指针,a1是基类指针,根据RTTI机制,从p1所指向的类People类向上找,能找到a1所指的类Artist类,所以,dynamic_cast转换是合法的。
void test3() {
People *p1 = new People();
printf("\ndynamic_cast test:\n");
Musician *m1 = p1; //success
People *p2 = (People*)m1; //cannot cast 'Musician *' to 'People *' via virtual base 'Musician'
People *p3 = dynamic_cast<People*>(m1);//error: 'Musician' is not polymorphic
}
分析:
根据dynamic_cast使用注意事项中的“dynamic_cast转换符只能用于含有虚函数的类(虚基类
)”, Musician是非虚基类,所以,无法进行强制dynamic_cast转换。
void test4() {
People *p1 = new People();
printf("\ndynamic_cast test:\n");
Teacher *t1 = p1; //success
People *p2 = (People*)t1; //success,继承自Teacher,采用非虚继承的方式(不使用virtual),所以ok。
People *p3 = dynamic_cast<People*>(t1); //success:加了dynamic_cast,进行强转
}
分析:
1)对于
People *p2 = (People*)t1;
People继承自Teacher,而采用非虚继承的方式,成功,因为采用的是非虚继承;
2)对于
People *p3 = dynamic_cast<People*>(t1);
加了dynamic_cast,进行强转,成功,因为Teacher是虚基类,而且符合RTTI规则。
dynamic_cast源码举例:
在Android源码中,可以查看到很多dynamic_cast的实现,来看其中一例:
代码位置:
/ndk/sources/cxx-stl/gabi++/src/dynamic_cast.cc
部分代码摘抄:
(从函数名字就可以看出,这个函数的作用是:把基类转换成派生类)
// based-to-derive cast in the general case.
void
base_to_derived_cast(const void *object,
const abi::__class_type_info *type,
cast_context* context)
{
const void* saved_dst_object = context->dst_object;
bool is_dst_type = *type == *context->dst_type;
if (is_dst_type)
context->dst_object = object;
if (object == context->object
&& context->dst_object != NULL
&& *type == *context->src_type)
{
if (context->result == NULL)
context->result = context->dst_object;
else if (context->result != context->dst_object)
context->result = ambiguous_object;
context->dst_object = saved_dst_object;
return;
}
switch(type->code())
{
case abi::__class_type_info::CLASS_TYPE_INFO_CODE:
// This isn't not the class you're looking for.
break;
case abi::__class_type_info::SI_CLASS_TYPE_INFO_CODE:
// derived type has a single public base at offset 0.
{
const abi::__si_class_type_info* ti =
static_cast<const abi::__si_class_type_info*>(type);
base_to_derived_cast(object, ti->__base_type, context);
break;
}
case abi::__class_type_info::VMI_CLASS_TYPE_INFO_CODE:
{
const void* vtable = get_vtable(object);
const abi::__vmi_class_type_info* ti =
static_cast<const abi::__vmi_class_type_info*>(type);
// Look at all direct bases.
for (unsigned i = 0; i < ti->__base_count; ++i)
{
if (!ti->__base_info[i].is_public())
continue;
const void *subobject =
get_subobject(object, vtable, &ti->__base_info[i]);
base_to_derived_cast(subobject, ti->__base_info[i].__base_type,
context);
// FIXME: Use flags in base_info to optimize search.
if (context->result == ambiguous_object)
break;
}
break;
}
default:
assert(0);
}
context->dst_object = saved_dst_object;
}
} // namespace