从下面代码可以知道,用reinterpret_cast将一种类型的指针转到另外一种类型的指针是不会出现任何错误的,但是仅仅是编辑器的错误哈哈。另外,注意用引用传参,节省copy 时间么。
// general case
template<class Return_Type, class Argument_Type>
inline Return_Type &force_cast(Argument_Type &rSrc)
{
return(*reinterpret_cast<Return_Type *>(&rSrc));
}
// specialization for const
template<class Return_Type, class Argument_Type>
inline const Return_Type &force_cast(const Argument_Type &rSrc)
{
return(*reinterpret_cast<const Return_Type *>(&rSrc));
}