Effective C + + clause 04: Determines that an object has been initialized before it is used

Source: Internet
Author: User

Remember:

For anything other than the built-in type, the initialization responsibility falls on the constructor, ensuring that each constructor initializes each member of the object.

C + + stipulates that the initialization action of a member variable of an object occurs before entering the constructor body.

Replace the assignment action within the constructor with the member initialization list.

If the member variables are const or reference, they must have an initial value and cannot be assigned.

The base class is initialized earlier than derived class, and the member variables of class are always initialized in the order in which they are declared, not affected by the order of the member initialization list.

To avoid the "initialization order across compilation units" issue, replace the non-local static object with the local static object.

Abentry::abentry (conststringconststring& address,                   Const list<phonenumber>& phones)    : thename (name),     theaddress (address),     thephones ( Phones),     numtimesconsulted (0) {    = name;          // These are assignments, not initialization    theaddress = address;     = phones;     0 ;}

Abentry::abentry (conststringconststring& address,                   Const list<phonenumber>& phones)    : thename (name),     theaddress (address),     thephones ( Phones),     numtimesconsulted (0) {}

abentry::abentry ()    : Thename (),             // Call the default constructor      of Thename Theaddress (),     thephones (),     numtimesconsulted (0) {}

Effective C + + clause 04: Determine 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.