Clause 4: Determine that the object was initialized before it was used (make sure, objects is initialized before they ' re used)

Source: Internet
Author: User

In fact, no matter what language to learn, or feel the need to form a declaration after use, first initialized and then use.

1. Always initialize the object before it is used.

Built-in type: must be done manually.

Outside the built-in type: Use the constructor to complete. Make sure that each constructor initializes a member of the object.

shit!! Read this, have always thought that some concepts alone consciousness, Daniel also has this idea!!!!

2. Differentiate open Assignment & initialization (this needs to look again, it seems that the previous cognition is wrong!!!!) )

Initializatin: Initialization of non-built-in types is done when the default constructor is in effect.

Assignment: A non-default constructor simply assigns a value to a member variable.

Be sure to include the user-defined types member variable in the member initial list. For memory, the built-in type is also placed in the initial value column.

3. Order of member initialization

The member variables of base classes ==> derived classes ==>class are initialized in order of declaration.

4.non-local Static object:

The life cycle of the static object, starting from creation until the end of the program.

The local static object refers to the static objects that are scoped within the function. Non-function scoped objects are non-local static objects, such as: File/global/namespace/classs ... The static object inside.

Compilation unit (translation unit): is a single source file + #include files (that is, header files)

There is no clear definition of the initialization order for non-local static objects defined within different compilation units. "= = This problem needs to be overcome, so there is already technology.

The essence is to use local static object instead of Non-local static object

is to put the non-local static object into the function, by calling the function (returning a reference to the local static object) instead of using the Non-local static object directly.

In fact, you initialize the local static object inside the function. And if this object is not actually used, it will naturally not lead to the construction and analysis of the composition.

eg

Class filesystem{...};

filesystem& TFS ()

{

Static FileSystem FS;

return FS;

}

Class directory{...};

Directory::D irectory (params)

{

....

std::size_t disks = TFS (). Numdisks ();

....

}

directory& TempDir ()

{

Static Directroy TD;

return TD;

}

To use it before the object is initialized, do this:

1. Manually initialize the built-in Non-member object.

2. Initialize all components of an object using the member initializer column (member initialization lists).

3. If "Initialization order uncertainty" is used, use non-local static object to function as local static object

The above three points are also note content.

Clause 4: Determine that the object was initialized before it was used (make sure, objects is initialized before they ' re 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.