C + + variable initialization issues

Source: Internet
Author: User

The difference between initialization and assignment

In C + +, the variable initialization and assignment operators are two completely different concepts.

Initialization is not an assignment, the meaning of initialization is to assign an initial value when creating a variable allocation storage space, and the meaning of the assignment is to erase the current value of the memory space and replace it with a new value.

List initialization in C + +

int number1 = 1

int number2 (1)

int Number3 {1}

int number4 = {1}

As a new standard for c++11, curly braces are used to initialize variables for full application, which is called List initialization (listing initialization)

List initialization features: Compiler error when initializing with list and the initial value is at risk of information loss

int Num{1}//correct

int num2{1.1}//Error

int NUM3 = 1.1//correct

Default initialization

A built-in type variable defined in the body of any function is initialized to 0, and the built-in type variable defined inside the function body will not be initialized, this value is undefined, it is an indeterminate value, using the value of an uninitialized variable is a wrong way of programming, and it is difficult to hang, It is recommended that variables be initialized when each built-in type variable is defined.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------

C + + variable initialization issues

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.