"Effective C + +"-clause 04: Determining that an object was initialized before it was used

Source: Internet
Author: User

Reading an uninitialized value results in ambiguous behavior. On some platforms, simply reading the uninitialized value may cause your program to terminate. It is more likely that a few "semi-random" bits are being read, polluting the object that is reading the action, resulting in unpredictable program behavior and many unpleasant debugging processes.

  • Manual initialization for objects of built-in type
  • For anything other than the built-in type, the initialization responsibility falls on the constructor.

But to distinguish between assignment and initialization. An initialization action for a member variable of an object occurs before the constructor body is entered.

In the above code, both thename,theaddress and Thephones are not initialized, but are assigned values. Initialization occurs earlier, when the default constructor of these members is called automatically (earlier than the time it takes to enter the Abentry constructor body). But this is not true for numtimesconsulted because it belongs to the built-in type and does not guarantee that the initial value must be obtained before the point in time that you see the assignment action.

A better method of Abentry constructor is to replace an assignment action with an initialized member list:

This constructor is more efficient. The version based on the assignment first calls the default constructor to set the initial values for thename,theaddress and Thephones, and then immediately assigns them a new value. All of the default constructors are wasted as a result. The member initialization list avoids this problem. Because the arguments that are set for each member variable in the initialization list are taken as arguments to the constructors of each member variable. In this example, the thename with name as the initial value of the copy construction, theaddress with address as the initial value of the copy construction, thephones to phones as the initial value of the copy construction.

For most types, it is more efficient to call the copy constructor once, rather than calling the default constructor at first and then invoking the copy assignment operator. For built-in objects, the cost of initialization and assignment is the same, but for consistency it is best done through member initialization lists.

"Effective C + +"-clause 04: Determining that an object was initialized before it was used

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.