The difference between Static and Const

static

  1. static local variable is a variable declared as local variables, then the local variable will not be released after the completion of function execution, but remain in memory
  2. static global variable represents a variable can be accessed globally current file
  3. static function represents a function can only be accessed in the current file
  4. static class member variable indicates that the class members are shared by the whole
  5. static class member function indicates that the function is common to the whole class, and can only access static member variables

const

  1. const constants: initialized when defined, can not be changed later.
  2. const parameter: func (const int a) {}; this parameter can not be changed in a function
  3. const modified class member function: which member variables can only be read-only operations

the role of the static keyword:

(1) Effect in vivo function of a static variable range for the body of the function, the variable memory is allocated only once, so its value remains at the last value on the next call; 

(2) static global variables and functions within the module can be accessed within the module functions, but other functions can not be accessed outside of the module; 

(3) static member variables in the class belonging to the entire class have all objects of the class only one copy; 

(. 4) in the static member function belongs to the class of the class have, this function does not receive this pointer, which can only access the static class member variables.

const keyword role:

(1) be changed to prevent a variable 

(2) a constant pointer and pointer constant declarations 

(3) const modified parameter, indicating that it is an input parameter, its value can not be changed within the function; 

(4) for the member function, which is specified when the const type, which indicates that it is a constant function, not modify the class member variables; 

(5) member function, which is sometimes necessary to specify the type of return value const, so that the return value is not "Left value."

 

static global variables and global variables common difference:

static global variable is initialized only once, to prevent the referenced file in the other unit.

static local variables and the difference between ordinary local variables:

static local variables initialized only once; the next value in accordance with the previous results.

static function and normal function What is the difference:

Only a static function in memory, general function to maintain a copy of each call.

 

Guess you like

Origin www.cnblogs.com/zonkidd/p/11728766.html