Effective C + + clause 27 Do as few transformation operations as possible

Source: Internet
Author: User

1. There are three types of transformation grammar,

C-style Transformation: (T) expression

Function Style Transformation: T (expression)

There are no differences between the two ways, except for the position of parentheses, which can be referred to as "legacy transformation."

C + + offers four new types of transformation:

const_cast<t> (expression):

Removes a const variable reference or pointer to a non-const reference or pointer, removing the constant attribute of the variable. T must be a pointer or reference.

Note: Although the variable after const_cast transformation can be changed, but because of "constant folding" (the C + + compiler replaces the const variable with the corresponding constant at compile time), the value of the original variable shows the unchanged nature (only the built-in type will have this behavior).

reinterpret_cast<t> (expression):

The variable bits is re-interpreted at the level, and there is no change to the binary storage method of the variable. T must be a pointer, reference, or arithmetic type.

dynamic_cast<t> (expression):

A transition between polymorphic classes (with virtual functions) during the execution period for runtime type recognition. T must be a pointer or reference, expression must return a polymorphic class type.

If T is a pointer, NULL is returned when the conversion fails, and if T is a reference, an exception is thrown when the conversion fails.

static_cast<t> (expression):

Most types of conversions can be performed, but static_cast cannot remove the const or volatile attribute compared to the const_cast type, which does not support runtime type recognition compared to dynamic_cast, and reinterpret_ Cast is not a re-interpretation at the bit level, but a re-encoding of the bits after they are changed.

2. Static_cast is similar to the function of the return value, and returns a temporary copy, for example:

int a;static_cast<double> (a) =2.0;

Does not change the value of a, so do not manipulate the converted object unless you convert to pointers and references like Dynamic_cast,const_cast.

In addition, dynamic_cast redirects the pointer address when the downcast or upcast succeeds (this is determined by the C + + object-oriented model), so dynamic_cast and static_cast operate differently on the conversion between class types.

3. Try to replace C's legacy transformation with the new C + + transformation operation, which is more precise in its classification and can clearly identify the transformation intent and facilitate the identification of errors.

If possible, try to avoid the transition operation, especially to avoid the operation of type recognition such as dynamic_cast, which results in low efficiency.

Effective C + + clause 27 Do as few transformation operations as possible

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.