Valid tive C ++ (4) determines that the object has been initialized before it is used

Source: Internet
Author: User

Hazard: Reading uninitialized values may lead to unclear or even semi-randomized behavior. Best Solution: Always initialize the object before using it; ensure that every constructor initializes every member of the object.

1. differentiate between assignment and initialization:


From the initialization perspective, this is not the best solution. Although this will allow the object to refer to the value you expect, in fact, the initialization action of the object's member variables takes place before entering the constructor body. In the constructor itself, values are assigned instead of initialization.

2. Better Syntax: Use member variables to initialize the list.

The result is the same as the final result of the previous one, but the efficiency is high.
Rule: always list all member variables in the Initial Value List to avoid missing member variables to be initialized. That is, the list of initialization members is used.
A special case once again confirms the value of the above sentence: if the member variables are const or reference, they must have an initial value and cannot be assigned a value.

3. Initialization order of members:
Generally:

Base classes is initialized earlier than its derived classes, while the class member variables are always initialized in the declared order.

Very low:
Initialization sequence of non-local static objects defined in different compilation units

Resolution: 1 non-local static: includes:
A global object is an object defined in the namespace scope and declared as static in the file scope within the classes.
A local static object is an object declared as static in a function.
2. compilation unit: generate the source code of a single target file. It can basically be considered as a source code file plus the header file it contains.
Problem:
C ++ does not clearly define the initialization order of "non-local static objects in different compilation units.
Cause:
It is quite difficult to determine their initialization order, or even unsolvable.
Solution:
Reference-returning function (example)
Principle:
C ++ ensures that the local static object in the function will be initialized when the definition of the object is first met during the call of the function. Therefore, replacing direct object access with function calls ensures that a reference of an initialized object is obtained.
Demo: versions with initialization order issues:
A. cpp

B. cpp


Improved Version:


Summary: manual Initialization is performed for built-in type objects, so that C ++ does not guarantee initialization of them. It is best to use the member variables to initialize the list of constructors, the sorting order should be the same as the declared order in the class. To avoid the issue of "initialization order across compilation units", please use the local static object (referance returning function) replace the non-local static object.
References: Objective C ++ 3rd

Related Article

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.