Four types of conversion const_cast in C + + can change constants

Source: Internet
Author: User

We have four specific castingoperators:dynamic_cast, reinterpret_cast, static_cast and const_cast. Their format is to follow the new type enclosed between angle-brackets (<>) and immediately after, the expression to be converted between parentheses.

dynamic_cast <new_type> (expression)
Reinterpret_cast <new_type> (expression)
Static_cast <new_type> (expression)
Const_cast <new_type> (expression)

One, to C + + four types of conversion summarized as follows:

Const_cast (expr)

Constant for removing objects (Cast away the constness)

Const_cast is generally used for pointers or references

The purpose of using const_cast to remove a const qualifier is not to modify its contents

Use Const_cast to remove a const qualifier, usually for a function to accept this actual argument

static_cast (expr)

Any type conversions implicitly executed by the compiler can be completed by static_cast

When a larger arithmetic type is assigned to a smaller type, it can be cast with static_cast.

You can convert a void* pointer to a pointer of a type

You can convert a base class pointer to a derived class pointer

Cannot convert const to Nonconst, this only const_cast can be done

Reinterpret_cast (expr)

"Typically provides a lower layer of interpretation for the bit pattern of an operand" means to interpret the data in the form of binary existence.

int i;

Char *p = "This is a example.";

i = reinterpret_cast<int> (p);

At this point, the value of I and P is exactly the same.

int *ip

Char *pc = reinterpret_cast<char*> (IP);

Programmers need to remember that the real object the PC points to is int, not string.

If you manipulate your PC as a character pointer, you may cause a run-time error

such as int len = strlen (PC);

dynamic_cast (expr)

Perform a "security down" transition operation, which means supporting the Run-time identification pointer or the object being pointed to, which is the only transition operation that cannot be done in the old-fashioned language.

Dynamic_cast is the most stringent conversion, static_cast second, and reinterpret_cast is the most relaxed. If you encounter a problem where you can't turn an integer into a function pointer, you can solve it like this:

Reinterpret_cast<lpfun&> (naddress);

Note that lpfun here has a "&" symbol, refers to a reference, C + + reference is actually implemented with pointers, and these "transformations" are actually the conversion of pointers, so plus the reference symbol compiled to pass.

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.