Summary of C + + technical issues-No. 0 type conversion

Source: Internet
Author: User
Tags type casting

Start today with a summary of how C + + often uses technology.

Reference books: "C++primer", "C + + object Model", "design Mode", "Windows core Programming", "STL decryption".


1. detailed meaning of four cast of static_cast

C + + type conversions: implicit conversions and explicit conversions.

Explicit conversions:static_cast,dynamic_cast,reinterpret_cast, Const_cast.


1.1. static_cast

How to use:static_cast<type-id> (expression)

Description: The operator converts expression to the type-id type, but does not perform a type check to guarantee the security of the conversion.

Source: Why do I need static_cast type casting?

(1) Void pointer , other type of pointer.

(2) Change the usual standard conversions.

(3) Avoid ambiguity that may occur in multiple conversions.

Several ways to use:

(1) A pointer or reference conversion between a base class and a subclass in a class hierarchy.

It is safe to make an upstream conversion (a pointer or reference to a class is converted to a base class).

When you make a downstream conversion (a pointer or reference to a base class is converted to a subclass), it is unsafe because there is no dynamic type checking.

(2) For conversions between basic data types, such as converting an int to char .

The security of such conversions also needs to be ensured by the developer.

(3) convert the void pointer to a pointer of the target type (unsafe).

(4) Convert any type of expression to void type.

1.2. dynamic_cast

How to use:dynamic_cast<type-id> (expression)

Description: This operator converts expression to an object of type Type-id. Type-id must be a pointer, reference, or void* to a class .

A pointer or reference to a base class type that is safely converted to a pointer or referral of a derived type.

Run two operations at a time. It first verifies that the requested conversion is valid, that only the conversion is valid, and that the operation characters the actual conversion.

Flow chart.


Source: Why do I need to dynamic_cast?

Simply put, when you can't use the virtual function. You can only do this if you cannot change the source code.

Thedynamic_castandstatic_cast effects are the samewhen upstream conversions are made between class hierarchies, and when the downstream conversion is performed, thedynamic_cast is a feature that has type checking.


1.3. reinterpret_cast

How to use:reinterpret_cast<type-id> (expression)

Description:Type-id must be a pointer, reference, arithmetic type, function pointer, or member pointer.

How to use: it is able to convert a pointer to an integer, or to do the opposite.


1.4. Const_cast

How to use:const_cast<type-id> (expression)

Description: This operator is used to change the const or volatile property of a type .

A constant pointer or reference is converted to a pointer or reference, and still points to the original object.

Constant objects are converted to very mass objects.

Personal understanding: Personal infrequently used, this method of use violates the original design intent.

Demo Sample:

Class B {public:int m_inum;}; void foo () {    const B B1;    B1.m_inum = 1000;//Compilation failed because B1 is a constant    b& b2=const_cast<b&> (B1);    The M_inum value of B2.M_INUM=200;//FINE,B1 and B2 is 200}

Summary of C + + technical issues-No. 0 type conversion

Related Article

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.