A static_cast is also useful to perform a conversion that the compiler will not generate automatically. For example, we can use a static_cast to retrieve a pointer value that was stored in a void* pointer (Section 4.2.2, p. 119):
- void* p = &d;
-
double *dp = static_cast<double*>(p);
When we store a pointer in a void* and then use a static_cast to cast the pointer back to its original type, we are guaranteed that the pointer value is preserved. That is, the result of the cast will be equal to the original address value.
- reinterpret_cast
A reinterpret_cast generally performs a low-level reinterpretation of the bit pattern of its operands.
A reinterpret_cast is inherently machine-dependent. Safely using reinterpret_cast requires completely understanding the types involved as well as the details of how the compiler implements the cast.
注意:reinterpret_cast 是最“野蛮”的强制类型转换,一般应尽量避免使用。它在Sockets 网络编程(www.cppentry.com)中有一个常见的用途:将struct
sockaddr_in* 强制转换为struct sockaddr*。当然,这个转换可以分为两步来做,先将struct sockaddr_in* 隐式转换到void*,再static_
cast 到struct sockaddr*。
As an example, in the following cast
- int *ip;
- char *pc = reinterpret_cast<char*>(ip);
the programmer must never forget that the actual object addressed by pc is an int, not a character array. Any use of pc that assumes it’s an ordinary character pointer is likely to fail at run time in interesting ways. For example, using it to initialize a string object such as
- string str(pc);
- is likely to result in bizarre run-time behavior.
The use of pc to initialize str is a good example of why explicit casts are dangerous. The problem is that types are changed, yet there are no warnings or errors from the compiler. When we initialized pc with the address of an int, there is no error or warning from the compiler because we explicitly said the conversion was okay. Any subsequent use of pcwill assume that the value it holds is a char*. The compiler has no way of knowing that it actually holds a pointer to an int. Thus, the initialization of str with pc is absolutely correct—albeit in this case meaningless or worse! Tracking down the cause of this sort of problemcan prove extremely difficult, especially if the cast of ip to pc occurs in a file separate from the one in which pc is used to initialize a string.
ADVICE: AVOID CASTS
By using a cast, the programmer turns off or dampens normal type-checking (Section 2.3, p. 44). We strongly recommend that programmers avoid casts and believe that most well-formed C++(www.cppentry.com) programs can be written without relying on casts.
This advice is particularly important regarding use of reinterpret_casts. Such casts are always hazardous. Similarly, use of const_cast almost always indicates a design flaw. Properly designed systems should not need to cast away const. The other casts, static_cast and dynamic_cast, have their uses but should be needed infrequently. Every time you write a cast, you should think hard about whether you can achieve the same result in a different way. If the cast is unavoidable, errors can be mitigated by limiting the scope in which the cast value is used and by documenting all assumptions about the types involved.