Valid C ++ Clause 27 and valid Clause 27

Source: Internet
Author: User

Valid C ++ Clause 27 and valid Clause 27
Minimize transformation actions

What is the purpose of doing as little transformation as possible? Obviously, it is nothing more than improving program stability and program execution efficiency.
So what are the transformation methods? What are the weaknesses of each method? This is the focus of our study in this section.
C ++ has four types of transformations:

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

Each type of transformation has the following functions:
1. const_cast is usually used to convert the constant feature of an object to the constness (cast away the constness ). It is also the only C ++-style transformation operator of this capability.
2. dynamic_cast is mainly used to execute "safe downcasting", that is, to determine whether an object belongs to a type in the inheritance system. It is the only action that cannot be executed by the old-style syntax and the only transformation action that may consume significant operation costs ).
3. reinterpret_cast intends to perform a low-level transformation. The actual action (result) may depend on the compiler, which indicates that it cannot be transplanted. For example, you can convert pointer to int into int, which is often used in low-level code. For example, we will discuss how to write a debugging allocator for debugging in raw memory (raw memory). For details, see section 50.
4. Execute forced implicit conversion (implicit conversions) for static_cast ). For example, convert int to double or non-const to constant. It can also be used to execute some conversion response transformations, but cannot convert const to non-const.

Let me summarize the above four points,
First, const_cast is used to remove the const feature.
Remove the const attribute: const_case

Class Window {public: virtual void onResize (){......}; ......}; Class specialWindow: public Window {public: virtual void onResize () {static_cast <Window> (* this). onResize (); // The call is a copy. ...... }};

The execution 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 becausestatic_cast<Window>(*this).onResize();This statement calls a copy of the base class and has nothing to do with the current object. The author emphasizes this part of content, but don't be dumb. Let's take a closer look at the code. His transformation is a copy! Consistency can be achieved with only a few changes. As follows:

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

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.