Effective C + + clause 27

Source: Internet
Author: User

Try to do less transformational action

What is the purpose of doing less transformational action? Obviously, it is to improve the stability of the program and improve the efficiency of the program execution.
So, what are the ways of transformation? What are the weaknesses in each of these ways? This is the focus of our study in this section.
There are four types of transformation in C + +:

const_cast<T>(expression)dynamic_cast<T>(expression)reinterpret_cast<T>(expression)static_cast<T>(expression)

The role of each transformation is as follows:
1.const_cast is often used to remove the constant characteristics of an object (cast away the constness). It is also the only C++-style transition operator with this capability.
2.dynamic_cast is primarily used to perform "safe downcasting", which is used to determine whether an object belongs to a type in the inheritance system. It is the only action that cannot be performed by old-fashioned syntax, and it is the only transformational action that can cost significant operational costs (in the following detail).
3.reinterpret_cast intent to perform a low-level transformation, the actual action (result) may depend on the compiler, which indicates that it is not portable. For example, to convert pointer to int into int, such transformations are often used in low-level code. For example, the discussion discusses how to write a debug allocator for raw memory (debugging allocator), see clause 50.
4.static_cast performs a forced implicit conversion (implicit conversions). For example, convert int to Double,non-const to constant. It can also be used to perform some conversion of the echo conversion, but cannot convert the const to Non-const.

Let me summarize the above four points,
First, the const_cast is a function of removing the const feature.
Remove the const attribute: const_case

class Window{public:    virtualvoid onResize(){……};    ……};class specialWindow:public Window{public:    virtualvoid onResize(){        static_cast<Window>(*this).onResize();//调用的是副本。        ……    }};

The result of the above code is that the data in the base class has not changed and the data in the derived class has changed. This is because static_cast<Window>(*this).onResize(); this sentence is called a copy of the base class, regardless of the current object. The author stresses this part of the content, but, we do not become silly, we carefully look at the code, he transformed the original is a copy! Consistency can be achieved with just a little change. As follows

class Window{public:    virtualvoid onResize(){……};    ……};class specialWindow:public Window{public:    virtualvoid onResize(){        static_cast<Window*>(this).onResize();//ok        ……    }};

Effective C + + clause 27

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.