Explore C + + default initialization

Source: Internet
Author: User

As described in the 40th page of C + + primer 5th, if you define a variable without specifying an initial value, the variable is initialized by default, and the variable is given a "default."

Depending on the position defined by the variable, there are two situations:

1. Variables defined outside of any function body are initialized to 0.

2. The built-in variables defined inside the function body will not be initialized.

Here are two things that should be very clear, but when I learned about classes, I still had questions about how member variables in the class were initialized.

In writing the exercises, I studied it clearly.

The default initialization of a member variable within a class is related to the location of the object defined by the class, in two cases:

1. An object defined in a class other than the body of any function whose member variable is initialized to 0.

2. The object of the class defined within the function body, will not be initialized, the value is random.

Looking back at the default initialization of variable definitions, you can see that the case of member variables in the class is consistent with ordinary variables.

Explore C + + default 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.