Staticstatic modifier local variablestatic modifier Local variables are used to modify where variables are stored, from automatic variables to static variables (space in the static area, not on the stack), but the link properties and scope of the variables are not affected. When static modifies a local variable, it is created before the program executes and persists throughout the program's execution space, even if the scope is hidden and the variable is not destroyed. Take a look at the following two sections of codecode One: local variables with no static modifiers
int main () {int i=0;for (i=0;i<10;i++) {int j=10;j+=1;printf ("%d", j); J with no static modifier is 10}system ("pause") every time; return 0;}
code two: static modified local Variables
int main () {int i=0;for (i=0;i<10;i++) {static int j=10;j+=1;printf ("%d", j); J with static modifier is the value}system ("pause") after the last +1, and return 0;
from the above two pieces of code can see that the static modifier local variable does change the storage location of the variable, stored in the static area, the scope is not destroyed, so code two J each time is added 1 after the valuestatic modifier global variables and functionsThe static modifier and global variables are used to modify the link property of a variable from extern to internal, but the storage type and scope of the variable are not affected, and the static modified global variables are accessible only in their source files. cannot be accessed even if external files are using externOkay, the basic introduction to static is here, let's take a look at a static example.
int Count=3;int Main () {int i=0,sum=0,count=2;for (i=0,sum=0;i<count;i+=2,count++) {static int count=4;count++;if (i %2 = = 0) {extern int count;count++;sum+=count;} Sum+=count;} printf ("%d%d\n", count,sum); 4 20system ("pause"); return 0;}
is it a little dizzy to see this problem? I felt a little scared at first, because there were three different variables in storage: Global, local, static, and the key is that it's also a matter of time to use count. It's easy to figure out which count to call at each use, but don't be afraid, Let's analyze it slowly. Note: 1). When the authority variable and the global variable conflict, the local variable takes precedence, when the global count and the local count are present at the same time, the local variable count is given precedence.2). extern declaration variable makes the variable have external link property3). Static modifier local variable (does not destroy the variable out of scope is hidden)ConstWhy does Mulao have a const keyword? And what Mulao can be modified by const? What is Mulao different from the const modifier? The following are mainly discussed in respect of these three issues. to solve the first problem, you need to understand the macros and identifiers defined by define, and see the links below for details. http://blog.csdn.net/qq_34328833/article/details/52144447after probably understanding the identity macros defined by the Define, we will find many disadvantages of the macro defined by define: macros cannot be debugged , macros are replaced directly, and waste space when more is replaced ; Although a macro can be typed but the macro itself is untyped, the macro has side effects ; And these shortcomings can be solved by the Const. As you can see from the code below
#define MAX 5 //define The identifier that is defined by const int MAX=10 //is not placed in memory at this time but is placed in the symbol table int main () {int i=max; An identifier substitution int j=max during precompilation; The memory is allocated for J at this time, and int M=max is no longer assigned; Once again, replace the macro, then allocate the memory int n=max; No longer memory provisioning
because the compiler typically does not allocate storage space for ordinary const read-only variables, it saves them in the symbol table, which makes the variable a compile-time value, without the storage and read memory operations, which makes it more efficient. This solves the reason why Mulao in C + + with a const instead of define-defined identifiers and macros.Const modifier Variable Const modifies a variable when const can be on the left side of the type or on the right side of the type, such as
const int A=10;int const A=10;
The const-modified variable in the C language makes the variable steady but still a variable (const-modified variable in C is a constant variable)however, in C + + in order to eliminate the shortcomings of the macro also need to define constants, at this time the const-modified variable to remove its properties in C variables, fully upgraded to a constant amount (const-modified variables in C + + are constants) such as the following code:
const int A=10;int arr[a]={0};
the length of the array must be a value that represents a constant, and if it can be run under the. cpp, it can be explained that the const-modified variables in C + + are not run in. c files and can be run under. cpp , which means that the C + + A const-modified variable already does not have the nature of the variable, it is completely a constantConst modifier pointer
const modifier level pointer
1). Const on the left of *
int a=10;int B=20;const int *pa=&a;*pa=20; Fatal error a=20;pa=&b;
the const modifier is *PA, indicating that the pointer pa is pointing to the content can not be modified, but the PA pointer can be pointed to the variable can also change the pointer variable itself;
2). Const on the right side of *
int A=10;int b=20;int *const pa=&a;pa=&b;//(Error) *pa=b;
The const modifier is the PA, which indicates that the pointer variable itself cannot be modified, but can modify what the pointer points to;
3). The const is on the right side of *
int a=10;int b=20;const int *const pa=&a;pa=&b;//(Error) *pa=b;//(Error)
at this point, the const modifier *PA also modifies the PA, indicating that the pointer to the content is not modifiable, the pointer variable itself can not be modifiedTips
for helping with memory: if we treat PA and a as a male and female friend, girl A can be changed as boyfriend can change the address of the variable itself, schoolgirl a can spend a boyfriend's money like can modify the contents of the variable; the const on the left side of the * is like this girl a can change her boyfriend's money, but not to spend it. Const on the right side of the * is similar to this girl A is can spend a boyfriend money but can not change boyfriend, const if that is in the left side of the * Also on the right side that this girl can be miserable, that can not change boyfriend also can not spend boyfriend's money, is not feeling very interesting?
const modifier Level two pointer
1). Const on the left of the first *
int a=10;int b=20;int *pa=&a;int *pb=&b;const int **ppa=&pa;**ppa=b;//(Error) ppa=&pb;*ppa=&b;a=40;
Point to a relationship such as: The const modifier on the left side of the first *, minus the type we found that it modifies the **PA, indicating that we are not allowed to modify the value of a by pointing to the two-level pointer PPA in the a space, but can change the contents of the space by a, or modify the PPA pointer variable itself
2). Const is placed in the middle of two *
int a=10;int b=20;int *pa=&a;int *pb=&b;int *const *ppa=&pa;ppa=&pb;*ppa=&b; (error) **ppa=b;
The const modifier is in the middle of two *, according to the nearest principle, we know that the Const modifier is *ppa, which means that we can not change the contents of the space PA pointed by the PPA at this time, but we can modify the PPA pointer variable itself, can also modify the value of the two-level pointer ppa to space a
3). Const on the right side of the second *
int a=10;int b=20;int *pa=&a;int *pb=&b;int **const ppa=&pa;ppa=&pb; (Error) *ppa=&b;**ppa=b;
Const placed on the right side of the second *, according to the nearest principle, we know that the Const modifier is the PPA, stating at this time we are not able to modify the two-level pointer PPA pointer variable itself address, we can modify the two-level pointer PPA point to the space PA and PA points to space a contentconst modifier Level Two pointers there are still a lot of cases, the above only a const modifier of the two-level pointer, as long as the key points, it is clear what the const modifier is Mulao? Second-level pointers themselves? The second level pointer to the content of the first level pointer? Second-level pointer to the address of the first-level pointer? The second-level pointer indirectly points to the contents of the variable? As long as this is clear, you can harness the const modifier pointer problem very well. Const-Decorated reference
1). The const-modifier reference is the alias of the variable that does not have permission to modify the contents of the variable and can only be modified by that variable
int A=10;const int &refa=a;refa=20; (error) a=20;
2). The permissions of the const modifier variable can be scaled down but cannot be expanded
const int A=10;int &refa=a; (error) int B=20;const int &refb=b;
3). const modifies the global variable, which is stored in the read-only data area, and cannot be modified even if the pointer is used, and does not necessarily allocate space when it is defined
const int n=10; CONST modifier global variable int main () {int arr[n]; As long as the n is constant also unallocated space const int *p=&n; The space system ("pause") is allocated for n at this time; return 0;}
assigning the address of a const-decorated global variable to a pointer, that is, when used
4). Constants have constancy, only constant references can reference constants
int &refa=5; (error) const int &refb=5;
5). The type of the reference does not match
Double D1=3.1;const int &refd1=d1; 3d1=6.95;
Note: 1). A temporary variable is present when allocating space to a variable in memory, and the temporary variable is persistent (the default type is const int)2). When the name of the variable itself does not match the type of the alias, the reference is not D1, but the temporary variable3). Since a temporary variable is used, it will be destroyed, so the value of the variable D1 will not affect the value of REFD1.
6). The parameter of the const modifier function, the return value of the decorated function:
you can add a const modifier to a function's arguments when you want to not be able to modify the value of the variable arbitrarily inside the function.The const can also modify the return value of the function, when the return value of the function is not modifiable, or when it is necessary to return a constant value, you can use the Const modifier's return value, such as the six default member functions of the class copy constructor is a common reference, the date class as an example of the copy constructor /c8>
Date (const date& D) : _year (d._year), _month (D._month), _day (D._day) {}
Const modifier member function The
const modifier member function, followed by the member function, const,const the object pointed to by this pointer, which guarantees that the object that called the member function will not be changed within the function
to observe the specific value-passing procedure of a const-modified member function with the date class as an example
Date Class
#define _crt_secure_no_warnings 1#include<iostream>using namespace Std;class date{public:date (int year,int Month,int day): _year (year), _month (month), _day (day) {}void Display () const{cout<<_year<< "-" <<_ month<< "-" <<_DAY<<ENDL;} void Display () {cout<<_year<< "-" <<_month<< "-" <<_DAY<<ENDL;} Private:int _year;int _month;int _day;}; int main () {Date D1 (2016,8,10);d 1. Display (); const Date D2 (D1);d 2. Display (); System ("pause"); return 0;}
There are also the following issues, which are the permissions of the const modifier variables we mentioned earlier (permissions can be narrowed but not expanded)1). Const object can not call a non-const member function, you can call the const member function2). Non-const objects can call non-const member functions, or you can call the const member function3). The const member function can call other const member functions and cannot call non-const member functions4). Other member functions can be called within a non-const member function, or non-const member functions can be called This is a little bit of my personal understanding, if there are readers know more usage please contact me, i email [email protected]
Detailed keyword Static,const,volatile