相关疑难解决方法(0)

什么时候应该使用static_cast,dynamic_cast,const_cast和reinterpret_cast?

有什么用途:

  • static_cast
  • dynamic_cast
  • const_cast
  • reinterpret_cast
  • C风格演员 (type)value
  • 功能式演员 type(value)

如何决定在哪些特定情况下使用哪个?

c++ pointers casting c++-faq

2367
推荐指数
9
解决办法
52万
查看次数

通过void*而不是使用reinterpret_cast进行转换

我正在读一本书而且我发现reinterpret_cast不应该直接使用,而是将其与无效*结合使用static_cast:

T1 * p1=...
void *pv=p1;
T2 * p2= static_cast<T2*>(pv);
Run Code Online (Sandbox Code Playgroud)

代替:

T1 * p1=...
T2 * p2= reinterpret_cast<T2*>(p1);
Run Code Online (Sandbox Code Playgroud)

但是,我找不到解释为什么这比直接演员更好.如果有人能给我一个解释或指出我的答案,我将非常感激.

提前致谢

ps我知道用的是什么reinterpret_cast,但我从未见过以这种方式使用它

c++ casting void-pointers language-lawyer reinterpret-cast

37
推荐指数
2
解决办法
5697
查看次数

C++什么时候我们更喜欢在reinterpret_cast上使用两个链式的static_cast

首先,这不是重复的为什么当两个链接的static_cast可以完成它的工作时,我们在C++中有reinterpret_cast?.

我知道我们甚至不能使用两个连锁static_cast来实现这reinterpret_cast一点的情况.但是,在任何情况下我都应该选择两个链接static_cast而不是简单且更具可读性的情况reinterpret_cast吗?

c++ casting static-cast reinterpret-cast

8
推荐指数
3
解决办法
1065
查看次数