The const object is the local variable of the file by default.

Source: Internet
Author: User

 

When a non-const variable is defined in the global scope, it can be accessed throughout the application. If it is a const variable, it is only a local variable in the current file.

We know that if you want to use a variable in other files, we can use extern to identify the variable. The default non-const type variable is extern.

1. cpp

Extern int number; // declares a number variable, which is used to search for definitions in other modules.

Int main (){

Cout <number <Endl; // The number is 100

}

2. cpp

Int number = 100; // defines a global variable. The default value is extern.

If we use const

2. cpp

Const int number = 100; // is this row? If the const fails, the local variable is not extern by default, but the local variable of the file. Therefore, you must add extern.

Extern const int number = 100; // This OK

1. cpp

Extern const int number; // we know that const must be initialized, but if extern is declared, no Initialization is required.

Int main (){

Cout <number <Endl; // The number is 100

}

 

The const object is the local variable of the file by default.

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.