简体   繁体   English

reinterpret_cast

[英]reinterpret_cast

In the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book, and in chapter (8), it mentions the following about reinterpret_castC++ without Fear: A Beginner's Guide That Makes You Feel Smart一书中,在第 (8) 章中,提到了以下关于reinterpret_cast

....converts from one pointer type (int ) to another (char*). ....从一种指针类型 (int ) 转换为另一种 (char*)。 Because the cast changes the way the data pointed to is interpreted, it is called reinterpret_cast, as opposed to static_cast.*因为强制转换改变了指向数据的解释方式,所以它被称为 reinterpret_cast,而不是 static_cast。*

Can you describe this paragraph here?你能在这里描述一下这一段吗? Especially the reason for the way the operation is named?特别是操作命名方式的原因?

Thanks.谢谢。

Basically, reinterpret_cast reinterprets the bit pattern at a specific location as a different type.基本上, reinterpret_cast将特定位置的位模式reinterpret_cast解释为不同的类型。

See for example here: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05keyword_reinterpret_cast.htm例如,请参见此处: http : //publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc05keyword_reinterpret_cast.htm

The reinterpret_cast operator produces a value of a new type that has the same bit pattern as its argument. reinterpret_cast 运算符生成一个新类型的值,该值与它的参数具有相同的位模式。

A static cast converts the argument instead of just reinterpreting it. static cast转换会转换参数,而不仅仅是重新解释它。 You can try this out by static_casting an int to float and reinterpret_casting an int to float .您可以通过 static_casting an int to float和 reinterpret_casting an int to float来尝试这个。 The result will be totally different.结果将完全不同。

There's nothing fancy here.这里没什么好看的。 it's really just intended to reinterpret something.它真的只是想重新解释一些东西。

From standard 5.3.10, reinterpret_cast is aimed to cater the following cases:从标准 5.3.10 开始,reinterpret_cast 旨在满足以下情况:

  • A pointer can be explicitly converted to any integral type large enough to hold it.指针可以显式转换为任何足够大的整数类型以容纳它。
  • A value of integral type or enumeration type can be explicitly converted to a pointer.整数类型或枚举类型的值可以显式转换为指针。
  • A pointer to a function can be explicitly converted to a pointer to a function of a different type.指向函数的指针可以显式转换为指向不同类型函数的指针。
  • A pointer to an object can be explicitly converted to a pointer to a different object type.指向对象的指针可以显式转换为指向不同对象类型的指针。
  • Converting a pointer to a function into a pointer to an object type or vice versa is conditionally-supported.有条件地支持将指向函数的指针转换为指向对象类型的指针,反之亦然。
  • The null pointer value (4.10) is converted to the null pointer value of the destination type.空指针值 (4.10) 被转换为目标类型的空指针值。
  • A prvalue of type “pointer to member of X of type T1” can be explicitly converted to a prvalue of a different type “pointer to member of Y of type T2” if T1 and T2 are both function types or both object types.如果 T1 和 T2 都是函数类型或都是对象类型,则“指向类型 T1 的 X 成员的指针”类型的纯右值可以显式转换为不同类型的“指向类型 T2 的 Y 成员的指针”的纯右值。
  • An lvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast.如果可以使用 reinterpret_cast 将“指向 T1 的指针”类型的表达式显式转换为“指向 T2 的指针”类型,则可以将 T1 类型的左值表达式转换为“对 T2 的引用”类型。 That is, a reference cast reinterpret_cast < T& >(x) has the same effect as the conversion *reinterpret_cast< T* >(&x) with the built-in & and * operators (and similarly for reinterpret_cast< T&& >(x)).也就是说,引用转换 reinterpret_cast < T& >(x) 与使用内置 & 和 * 运算符的转换 *reinterpret_cast< T* >(&x) 具有相同的效果(对于 reinterpret_cast< T&& >(x) 也类似) .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM