Initialization of objects before they are used

Source: Internet
Author: User

the initialization action of a C + + specified object occurs before the body of the constructor is entered. (The initialization list is more efficient than initializing in the constructor body, and the default constructor is called for initialization in the function body)

The compilation unit refers to the source code that outputs a single target file.

Problem:

two source files, with at least one non-local static object in each file (that is, the object is global or in the namespace scope, or within class or file is declared as static within the scope of the If the initialization of a non-local static object within a compilation unit uses an non-local static object in another cell , at this time due to C + + there is no explicit provision for the initialization order of non-local static objects that are defined within different compilation units , so errors can be thrown.

Example:

You write the program (a compilation unit):

Class filesystem{public: ... size-tnumdisk () const; ...};
Extern FileSystem tfs;//ready for someone else to use the object


Someone else's program (another compilation unit)

Class directory{Directory (params) {size_tdisk=tfs.numdisks (); Using the object of the first compilation unit, assume that the previous file has been compiled prior to compiling the file. Yes, what if it's the opposite? }};


Workaround:

each non-local static object is moved to its own function (that is, declared as static within the function). These functions return a reference to the object it contains. Call these functions at the time of invocation instead of invoking the object directly. Justification (C + + guarantees that the local static object within the function is initialized when the function call first encounters the definition of the object)

After the program has been modified:

Class filesystem{public: ... size-tnumdisk () const; ...};   filesystem& TFS () {static FileSystem fs;  Returnfs;}   Class directory{Directory (params) {Size_tdisk=tfs (). Numdisks (); Call function TFS ()}};directory& TempDir () {Static Directory td;return TD;}

any kind of non-conststatic object whether he is local or non-local, in a multi-threaded environment to wait for something to happen will have trouble, The troublesome approach is to manually invoke reference-returned functions at the stage of the program's single-threaded start, which eliminates the racing form associated with initialization.

Summarize:

First, manual initialization of built-in types (C + + does not guarantee that they are initialized).

It is best to use the member initial column instead of the assignment within the constructor, and the order of the initial column should be the same as the order of declaration in class.

third, in order to avoid the problem of initialization of cross-compilation units, the non-local static object should be replaced with the local static object .




Initialization of objects before they are used

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.