Effective C + + study notes clause 26-29

Source: Internet
Author: User

Clause 26: Time to postpone variable definitions whenever possible

1. Throw an abnormal waste constructor halfway

2. Define variables within the loop, consume N constructors, n destructors, define variables outside the loop to consume N assignment functions, 1 constructs, one destructor;

Unless the assignment is consumed much less than construction and destruction, or if the code you are dealing with is highly sensitive, define the variable within the loop.

Clause 27: Try to do less transformational action

1, const_cast-----Out of the constant attribute, static_cast (implicit conversion display), dynamic_cast (from a base class pointer or reference of a hosted derived class to invoke the component of the derived class), reinterpret_cats low-level conversion

2, Static_cast<window> (*this). Onreise () function you might think that he called the function of the object base class, which actually calls the function of the copy of the object's base class. The access qualifier should be used instead.

3. To invoke the component of a derived class from a base class pointer or reference of a hosted derived class, you may be able to set a container to hold a bunch of (base class) smart pointers to the actual object of their derived class, and then call dynamic_cast to access the elements of the actual object

Instead of Method 1, set multiple smart pointers that point directly to different (derived classes) to point to the derived class object that you might want to use in a variety of depths "space becomes larger"

Instead of Method 2, use the base class to set an empty virtual function, "Sometimes it may not be very good"

4, if you can try not to use the transformation, especially in the efficiency of the code to avoid dynamic_cast, if the design needs to be transformed, try to change direction without transformation

5, if the transformation is necessary, try to hide him behind a function. The client can then call this function without having to put the transformation into their code.

6, the use of C + + new transformation, do not use old-fashioned, new classification to clear.

Effective C + + study notes clause 26-29

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.