Static_cast, const_cast, dynamic_cast, and reinterpret_cast

Source: Internet
Author: User
Http://blog.csdn.net/wfwd/archive/2006/05/30/763785.aspx
Static_cast is basically as powerful as C-style type conversion and has the same meaning. It also has functional limitations. For example, you cannot use static_cast to convert struct to int type or double type to pointer type like C type conversion. In addition, static_cast cannot remove the const attribute from the expression, because another new type conversion operator const_cast has this function.

Const_cast is used to convert the const or volatileness attributes of an expression. By using const_cast, you emphasize to people and compilers that all you want to do through type conversion is to change the constness or volatileness attributes of some things. This meaning is restricted by the compiler. If you try to use const_cast to modify the constness or volatileness attributes, your type conversion will be rejected.

Dynamic_cast, which is used to safely perform type conversion down the class inheritance relationship. This means that you can use dynamic_cast to convert a pointer or reference to a base class to a pointer or reference to its derived class or its sibling class, and you can know whether the conversion is successful. If the conversion fails, a null pointer is returned (when type conversion is performed on the pointer) or an exception is thrown (when type conversion is performed on the reference ).

Reinterpret_casts, using the type conversion operator, the conversion results are almost all execution period definitions (implementation-defined ). ThereforeCodeIt is difficult to transplant. The most common purpose of reinterpret_casts is to convert between function pointer types.

For example, the Code for converting function pointers cannot be transplanted (C ++ does not guarantee that all function pointers are represented in the same way ), in some cases, such conversions may produce incorrect results (see the m31 clause), so you should avoid converting the function pointer type.

5. If the compiler you use lacks support for the new type conversion method, you can replace static_cast, const_cast, and reinterpret_cast with the traditional type conversion method. You can also use the following macro replacement to simulate the new type conversion Syntax:
# Define static_cast (type, expr) (type) (expr ))
# Define const_cast (type, expr) (type) (expr ))
# Define reinterpret_cast (type, expr) (type) (expr ))

These simulations are not as secure as real operators, but when your compiler supports new type conversions, they can simplify the code upgrade process.

There is no easy way to simulate dynamic_cast operations. However, many function libraries provide functions to securely convert the type between the derived class and the base class. If you do not have these functions and you have to perform such type conversion, you can also return to the C-style type conversion method, but you will not be able to know whether the type conversion fails. Of course, you can also define a macro to simulate the dynamic_cast function, just like simulating other type conversions:

# Define dynamic_cast (type, expr) (type) (expr)

Remember, this simulation does not fully implement the dynamic_cast function. It cannot know whether the conversion fails.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.