The difference between C / C ++ Static Const with the

EDITORIAL:
This article aims to summarize the backup, to facilitate future inquiries, as is the personal summary, if wrong, please correct me; in addition, most of the content from the Internet, books, and various manuals, should please inform the infringement, immediately delete posts to apologize.

Article Source: Original poke me

static

1.static local variables to 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 in the global current file accessibility
3.static function represents a function can only be accessed in the current file
4.static class member variable indicates that members of the are common to the whole class
indicates that this function is common to the whole class 5.static class member functions, and can only be accessed static member variables
tip: Using this feature you can define variables with the same name and function of the same name in different files, without worrying about naming conflicts.

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 the function
3.const modified class member functions: the function to read-only operation member variables
Supplement some small knowledge: in general try to replace define with the const keyword; simply because define string replacement, no type checking, and a corresponding const data type is to be judged, and avoid some low-level errors. In addition, # define the code space occupied by the pretreatment, const define the space occupied by the data segment

  • : the effect of the static keyword
    ; scope (1) in vivo function of a static variable as a function thereof, the variable memory is allocated only once, so the value of the next call to the previous value remains
    in the module (2) the static global variables and functions that can be accessed within the module functions, but can not be accessed by other functions outside the module;
    (. 3) static member variables in the class have the entire class belonging to 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 action:
    (1) variable is changed to a stop
    (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 a function;
    (4) 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 it The return value is not "left value."

Guess you like

Origin blog.csdn.net/qq_42992084/article/details/84642107