C ++ style casting discriminant analysis

Reprinted from: https://blog.csdn.net/legalhighhigh/article/details/87459995

1.static_cast
for converting the parameters for a specified type. Note that, when cast, the compiler does not perform type checking to ensure safety. When downlink object class type conversion, dynamic type checking is not performed, and are therefore unsafe. static_cast provided null pointer interchangeable with any type of pointer.

2.dynamic_cast
usage and static_cast essentially similar. But during type conversion, the compiler will take the initiative to the type of security check. dynamic_cast also support up and down the line type conversion, but the compiler can perform type checking. When the lower line conversion, if not the parent class includes a virtual function, the compiler error. Further, dynamic_cast transition between the null pointer with the pointer does not generally supported.

3.const_cast
const_cast used to modify the (removed) object const, volatile and _unaligned properties, in addition, no difference variables before and after conversion even address in memory are also the same.

Guess you like

Origin www.cnblogs.com/laiyingpeng/p/11788004.html