Clause 27: Try to do less transformational action

Source: Internet
Author: User

The first is the four transformation operations provided by C + +:

1. Const_cast: Constant removal. 2. dynamic_cast: A safe transition to derived class can result in a high overhead of 3. Reinterpret_cast: Low-level transformation, for example, can be said to pointer to int, not recommended to use 4. Static_cast: Forced implicit conversions, such as int to const int,int to double, but const int to int only const_cast can do. One of the most common usage scenarios that we've seen before us is, like this:
1 classwindow{2  Public:3     Virtual    voidonResize () {...}4     ...5 };6 classSpecialwindow: Publicwindow{7  Public:8     Virtual voidonResize () {9Static_cast<window> (* This), OnResize ();Ten         ... One     } A     ... -}
But in fact the above copy does not have the desire to invoke the onresize () of its base class part, which in fact calls a separate onresize of the copy of the base class part of Specialwindow, in fact it has nothing to do with Specialwindow resize. At this point, if Specialwindow itself also attempts to modify the non-base section of Specialwidnow, then the object will enter a disabled state. The base class part did not make the necessary modifications, but the individual parts of the derived class underwent modifications. In this way, the object enters a state of disability. The correct way to invoke the resize of the base class is not to use the transformation operation at all:
class Specialwindow: public window{public:    virtualvoid  OnResize () {        window::onresize () {            ...        }        ...    }};
Again on the dynamic_cast, remember that the general call to bring the cost is still relatively large. The main case for dynamic_cast is that you want to have a single base class pointer to handle the derived class separately. There are generally two ways to replace dynamic_cast to achieve this effect. The first method is to take a container and store it in it, pointing directly to the pointer to the derived class object (often a smart pointer).
typedef vector<shared_ptr<specialwindow>> VPSW; VPSW Windptrs;  for (Vpsw::iterator iter = windptrs.begin ();     ! = Windptrs.end (); + +iter) {    iter-blink ();}
Another way to do this is to extend the virtual function from the derived class up another layer, so that directly using the base class pointer, you can call the derived class function. Keep in mind that good code is rarely used for transformational operations and should be isolated as much as possible. Summary: 1. If you should try to minimize the use of transformation, especially dynamc_cast (which consumes a lot of resources). 2. If you need to transform, then at least you should hide him, for example, put in a function to use. 3. If you use transformation, be sure to use the new transformation.

Clause 27: Try to do less transformational action

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.