Valid tive C ++ Clause 4: determines that the object has been initialized before it is used.

Source: Internet
Author: User

The initialization action of the object's member variables takes place before entering the constructor body.

Abentry: abentry (const STD: string & name, const STD: List <phonenumber> & phones)

{

Thename = Name; // All values are assigned, not initialization.

Thephones = phones;

Numtimesconsulted = 0;

}

This constructor first calls the default constructor to set an initial value for thename and thephones, and then immediately assigns them a new value.

Abentry: abentry (const STD: string & name, const STD: List <phonenumber> & phones)

: Thename (name), // These are Initialization

Thephone (Phones ),

Numtimesconsulted (0)

{}

In this example, the constructor performs copy construction based on the initial values such as name.

Class member variables are always initialized in the declared order. It is irrelevant to the order in the initialization list.

A compilation unit is the source code that generates a single target file. It is basically a single source code file with its header files.

Static object. The Destructor is automatically called at the end of main.

Static objects in the function are called local static objects. Other objects include global objects and are defined in the namespace scope, objects declared as static within the class and in the file scope are called non-local static objects.

C ++ does not explicitly define the initialization sequence of "non-local static objects defined in different compilation units.

Class filesystem {// from your library

Public:

...

STD: size_t numdisk () const;

...

};

Extern filesystem TFs; // objects to be used by the customer

Class directory {// created by the library Client

Public:

Directory (Params );

...

};

Directory: Directory (Params)

{

...

STD: size_t disks = TFs. unmdisks (); // use a TFs object

...

}

 

Directory tempdir (Params)

Since tempdir and TFs are non-local static objects defined in different compilation units, it cannot be ensured that TFs is initialized before tempdir.

The solution to this problem is to move each non-local static object to its own exclusive function and change it to a local static object, which is a bit like the sigleton mode.

C ++ ensures that the local static object in the function will be initialized when the function is called and the definition of the object is met for the first time. Therefore, use a function call (returning a reference pointing to a local static object) to directly access the non-local static object. This ensures that an initialized object is obtained. In addition, if you have never called the non-local static object's "simulation function", it will never lead to constructor and destructor costs; real non-local static objects are not so cheap.

// Reference returning function to prevent initialization order problems

Class filesystem {...};

Filesystem & TFS () // This function is used to replace the TFS object

{

Static filesystem FS;

Return FS;

}

Class directory {...};

Directory: driecloud (Params)

{

...

STD: size_t disks = TFS (). numdisks ();

...

}

Directory & tempdir () // This function is used to replace the tempdir object

{

Static directory TD;

Return TD;

}

Any non-const static object, whether local or non-local, will be troublesome to "wait for something to happen" in a multi-threaded environment. One way to solve this problem is to manually call all the reference-returning functions during the single-thread startup phase of the program, which eliminates the race condition related to initialization ).

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.