Effective C + + study notes clause 4-7

Source: Internet
Author: User

Article 4: Determine that an object has been initialized before it is used

One

Variables may or may not be initialized under different circumstances.

Note the difference between initialization and assignment.

1. Implicit initialization does not occur in a built-in type in a class, and the ability to customize a default constructor is initialized by default.

Therefore, it is important to initialize the built-in type when constructing the class, and it is best to give the custom object display initialization to avoid wasting resources in the function body.

2. The built-in type is not initialized in the function body and is automatically initialized to 0 outside of the function body.

Two

1. Const and reference types must be initialized and cannot be assigned

Three

1, when the class is really a lot of constructors, and always want to repeat the initialization of some member data, you can consider those "assignment and initialization as good" data assignment into a often private tool function.

Four

1, C + + has a strict initialization order, the member initialization sequence and the order of declaration in the class is a good habit, otherwise there may be obscure errors

Five

The static object within the C + + Guarantee function is initialized the first time it is called, and the order of initialization is guaranteed by replacing a No_local-static object in another file.

1, in a multi-compilation file, the initialization of a class A object in a file depends on the data of a Class B object in another file, the initialization order is an issue because the compiler cannot determine which object of the class in the file can be initialized first. However, the initialization of a class A object depends on the tree flower of another Class B object.

Solution:

A instantiated object in Class A that is prepared for Class B definition, and extern a x changes into an inline with a return & function that defines a static a x, which initializes class B by this function, which guarantees the order in which the object is initialized between classes.

Effective C + + study notes clause 4-7

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.