C + + variable definitions with const, static, and #define keywords __c++

Source: Internet
Author: User
Tags function definition modifier

One, const

1. In the definition of variable

(1) const int a=10;

Description: The defined int type variable A cannot be modified and can only be used. A variable with the Const keyword can be defined as a global variable, but at this point the global variable link defined is an internal

(2) defined in #a. h: const int b=10;

A variable with the const keyword defined in the header file, this header file can be referenced by several different source files, without problems, the variable B defined in the compile-time header file, variable b in different source files is not a global variable, but a B variable is defined separately in different source files. B is equivalent to a global variable in its own source file, but the link is internal and can only be used for this source file.

(3) Declaring a const in a function or code block is a code block that is useful only when the program executes code in that block of code, which cannot be used outside the code block.

(4) If, for some reason, the programmer wants a constant to be linked externally, the extern keyword can be used to override the default internal link.

namely: extern const int states=50;

2. const in function definition

Const int& Fun (const int & a,int & B)

Description: Const int & means that the return value of the fun function is const, its return value cannot be modified, and its return value can only be assigned to variables of the same type with a const modifier.

Fun (const int & a,int & B) is a const modifier in the function's arguments, which means that parameter a cannot modify the value of variable A in the function, that is, it can only be used without modification. In addition, a parameter variable with a const modifier, a real parametric can be a variable without a const modifier, or a variable with a const modifier. A parameter variable without a const modifier, the real parametric can only be a variable with no const modifier, and cannot be a variable with a const modifier, otherwise, the real parametric is not guaranteed to be modified in the function. To prevent this, C + + prohibits assigning a const-modified variable to a variable that is not a const modifier.

Second, static

1. Variables with static modifications can be defined in the header file and can be referenced by multiple source files, and there is no problem with compilation. Because a variable with a static modifier is mapped in memory to a static store, its variables persist until the program is run. And is a global variable.

2. A variable with a static modifier can be a global variable, and its value can be modified in the program.

3. A variable with a static modifier defined in a function or code block is scoped to a code block, that is, the constant is useful only when the program executes code in that block of code, which cannot be used outside the code block. This is similar to the Const.

Third, #define defined variables

#difine定义的变量只是对变量的表达式简单替换而已.

Look at one of the following procedures to understand the meaning.

The function of the #define MAX_NUM 100+200//macros is simply a string substitution, max_num*10=100+200*10

int ntemp=max_num*10;

The result of the run is: ntemp= 2100

Note: Global variables: the variables defined outside main () are global variables and are mapped to static storage in memory, which is very different from local variables, which are stored in the dynamic storage area of memory, that is, on the stack.

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.