Use reinterpret_cast with caution, because the purpose of this thing is to fool the compiler. Its conversion only changes the interpretation method, but does not change the actual data. If t的类型是TYPE, then x=reinterpret_cast<>(t)Equivalent to *((TYPE*)(&x))=t, if you use reinterpret_cast to convert an int to a float, it just copies the binary data completely, and the result is meaningless... Of course, to take a step back , of course you can use reinterpret_cast to achieve the pointer conversion you want
and static_cast will check the type, so it can be used for conversion of built-in types (for example, from int to float, it can be converted according to semantics), but if it is for pointers, they must be types on the same inheritance tree to be able to communicate with each other. Conversion, and only upcasting (from subclass to base class) is guaranteed to be safe.
Since your char* has no inheritance relationship with the target pointer, then obviously you can only use reinterpret_cast or C风格强转
The questioner is struggling with the memory pool recently...
reinterpret_cast It is just a reinterpretation and does not actually involve address transformation. There is no problem when converting ordinary pointers, but when converting between parent and child class pointers, the result may be incorrect.
Use
reinterpret_cast
with caution, because the purpose of this thing is to fool the compiler. Its conversion only changes the interpretation method, but does not change the actual data. Ift的类型是TYPE
, thenx=reinterpret_cast<>(t)
Equivalent to*((TYPE*)(&x))=t
, if you usereinterpret_cast
to convert an int to a float, it just copies the binary data completely, and the result is meaningless... Of course, to take a step back , of course you can usereinterpret_cast
to achieve the pointer conversion you wantand
static_cast
will check the type, so it can be used for conversion of built-in types (for example, from int to float, it can be converted according to semantics), but if it is for pointers, they must be types on the same inheritance tree to be able to communicate with each other. Conversion, and only upcasting (from subclass to base class) is guaranteed to be safe.Since your
char*
has no inheritance relationship with the target pointer, then obviously you can only usereinterpret_cast
orC风格强转
The questioner is struggling with the memory pool recently...
reinterpret_cast
It is just a reinterpretation and does not actually involve address transformation. There is no problem when converting ordinary pointers, but when converting between parent and child class pointers, the result may be incorrect.