Use extern in C ++

Source: Internet
Author: User

Extern in C ++ refers to global meaning. It generally has two purposes:

1. Declare Variables

First, let's talk about the differences between the Declaration and definition:

① Variable definition: used to allocate storage space for the variable. You can also specify the initial value for the variable. In a program, variables have only one definition.

② Variable Declaration: used to indicate the type and name of the variable to the program. Definition is also Declaration: when defining a variable, we declare its type and name. You can declare a variable name without defining it by using the extern keyword. For example:

Extern int I; // declare a variable I, but it is not defined
Int I; // declare and define a variable I

The extern declaration is neither a definition nor a bucket. In fact, it only indicates that the variable is defined elsewhere in the program. Variables in the program can be declared multiple times, but can only be defined once.

Only when the Declaration is a definition can the Declaration have an initialization type, because only the definition can allocate storage space. The initialization type must have a bucket for initialization. If the Declaration has an initialization type, it can be considered as a definition, even if the declaration is marked as extern, for example:

extern int i = 0;

Although extern is used, this statement defines I, allocates and initializes the bucket. The initialization type can be included only when the extern declaration is outside the function.

Because the initialized extern declaration is treated as a definition, any subsequent definition of this variable is incorrect: for example:

Extern int I = 0; int I; // error, repeated Definition

Similarly, the subsequent extern declaration containing the initialization is also incorrect:

Extern int I = 0; // define extern int I; // declare only, correct extern int I = 0; // duplicate definition, error


2. Declare the const variable

The variable defined by the const qualifier is a constant. For example:

constinti = 0;

Define I as a constant and initialize it to 0. The left value cannot be modified. Any attempt to modify I will cause compilation errors. For example:

I = 1; // an error is reported. The const variable cannot be modified.

It is precisely because constants cannot be modified after definition, so they must be initialized during definition. For example:

Const int I = 0; const int j; // error. The const variable is not initialized.

When a non-const variable is defined in the global scope, it can be accessed throughout the program. We can define a non-const change in a file. If a proper declaration has been made, we can use this variable in another file:

// File_1.cppint I; // defines I // file_2.cppextern int I; // use the ii ++ defined in file_1;

Unlike other variables, unless otherwise specified, the const variable declared in the global scope is a local variable that defines the object's file. This variable only exists in that file and cannot be accessed by other files. By changing the const to extern, you can access the const object in the entire program. For example:

// File_1.cpp // define and initialize the const variable iextern const int I = 0; // file_2.cppextern const int I; // use the const variable iif (I = 0) defined in file_1) cout <"success! "<Endl; // use the const variable I defined in file_1






This article is from my study notes blog, please be sure to keep this source http://6924918.blog.51cto.com/6914918/1266936

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.