terpret_cast主要是将数据从一种类型的转换为另一种类型。所谓“通常为操作数的位模式提供较低层的重新解释”也就是说将数据以二进制存在形式的重新解释。比如:
int i;
char *p = "This is a test.";
i = reinterpret_cast(p);
此时结果,i与p的值是完全相同的。reinterpret_cast的作用是说将指针p的值以二进制(位模式)的方式被解释为整型,并赋给i.
二、RTTI
RTTI(Run-Time Type Information,通过运行时类型信息)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型。
RTTI提供了以下两个非常有用的操作符:
(1)typeid操作符,返回指针和引用所指的实际类型。
(2)dynamic_cast操作符,将基类类型的指针或引用安全地转换为派生类型的指针或引用。
typeid 操作符的结果是名为type_info的标准库类型的对象引用,类type_info在库头文件 typeinfo中定义。typeid 操作符可以与任何类型的表达式一起使用。
内置类型的表达式以及常量都可以用作 typeid 操作符的操作数。如果操作数不是类类型或者是没有虚函数的类,则 typeid 操作符指出操作数的静态类型;
如果操作数是定义了至少一个虚函数的类类型,则在运行时计算类型。
Base *bp;
? ? Derived *dp;
? ? // compare type at run time of twoobjects
? ? if (typeid(*bp) ==typeid(*dp)) {
? ? ? ? // bp and dp point to objects of thesame type
? ? }
? ? // test whether run time type is aspecific type
? ? if (typeid(*bp) ==typeid(Derived)) {
? ? ? ? // bp actually points to aDerived
? ? }//特别注意,只有当 typeid 的操作数是带虚函数的类类型的对象的时候,才返回动态类型信息.
type_info的复制构造函数以及赋值操作符都定义为 private,所以不能定义或复制 type_info类型的对象。程序中创建 type_info 对象的唯一方法是使用 typeid 操作符。
type_info类确切定义随编译器而变化,但是标准保证至少实现operator==,operator=!,type_info::name()及type_info::before()。
运行时获知变量类型名称,可以使用 typeid(变量).name ,如:printf("%s\n", typeid(int).name()); 其输出不一定是 int ,我这里输出是 i。
对dynamic_cast 它究竟是怎么实现的呢?
最简单的方案就是将信息保存在vtable里,它会占用一个vtalbe表的项目,具体做法可以参考大作《Inside C++ Model》RTTI 的info 是如何和对象之间的关联。
举例
class Point
{
public:
? Point( float xval );
? virtual ~Point();
? float x() const;
? static int PointCount();
protected:
? virtual ostream& print( ostream &os ) const;
? float _x;
? static int _point_count;
};
其对应的内存模型:

通过上图可知 RTTI? info 存在于虚表的第一项。可以回答 “dynamic_cast转换符只能用于含有虚函数的类”,因为RTTI 依赖于虚表,所以用dynamic_cast 对应的类一定要有虚函数。