C + + variable initialization

Source: Internet
Author: User

When you define a variable without initialization, some variables are initialized.

Some variables: global variables, static variable namespaces (all of which are variables of built-in types). Local non-static variables are not initialized as non-static variables inside the function.

1 namespaceMySpace2 {3     intmInt;4 }5 6 intMain ()7 {8     //mySpace mint:09cout <<"mySpace mInt:"<< Myspace::mint <<Endl;Ten     return 0; One}


For non-built-in type variables, such as classes are initialized by constructor functions. When a class has a default constructor, the variable is not initialized, and the system may automatically be initialized with the default constructor (the default constructor is not a parameter), and the other point is that the system may be using the default constructor because the default constructor is not initialized when it is inaccessible: the default constructor is Private

1 classTmyclass2 {3  Public:4     //...5 Private:6Tmyclass (): _int (0) {cout <<"Tmyclass default constructor called"<<Endl;}7 Private:8     int_int;9 };Ten intMain () One { ATmyclass T1;//Error -System"Pause"); -     return 0; the}

It is said that variable initialization also has explicit and implicit points when the constructor of the learning class encounters

C + + variable 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.