In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "case Analysis of various variables and differences of C++". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "C++ variables and differences between the case analysis" bar!
In daily development, we often use variables and constants, such as the following code:
Int ser_1; / / global variable const int ser_2; / / global constant static int ser_3; / / static variable void main () {int serven_1; / / local variable const int serven_2; / / local constant static int serven_3; / / local static variable func (serven_1) } void func (int para) {/ / formal parameter int a;}
The above short piece of code covers variables, constants, and static variables, and defines both global and local. These are the ways we often use to define variables, but do you know the difference? What is their scope? How are they stored in memory?
I. Classification
Variables can be divided into: global variables, local variables, static global variables, static local variables.
In C++ 's memory management mechanism, global variables, static global variables and static local variables are all stored in static storage areas, and local variables are stored in the stack. If global variables and static variables are not initialized when they are defined, the compiler initializes these variables to 0.
II. Scope
Scope represents the scope of a variable in use, that is, within a certain range, the variable can be used normally, out of this range, the variable is invalid.
The scope of local variables: the scope of local variables is within the body of the function, out of the scope of the function is not used, like the serven_1 variable in the above code, can only be used in the main function, out of the main function can not be used. The shape parameter belongs to the local variable of the modulated function, and the actual parameter belongs to the local variable of the main tone function. Different functions can use exactly the same symbols to define variables, although the symbols are the same, but they are actually different. Like the following code, the symbol serven_1 of the two functions is different, one is the int type, and the other is the char type.
Void func_1 () {int serven_1 = 2;} void func_2 () {char serven_1 = 'sworn;}
Scope of global variables: the scope of global variables does not belong to a function, it belongs to a source program file, the scope is the entire source program, when you need to use global variables in a function, you need to make a declaration, generally using extern to make a declaration. Of course, there is no need to make an extern declaration in the same file. Global variables and local variables can be defined with the same symbol. In the function body that defines the local variable, it is considered to be a local variable if the symbol is used. As in the following code, serven_ 1 is used in the function func.
Int serven_1; / / global variable, which the compiler defaults to 0void func () {int serven_1 = 3; std::cout when uninitialized
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.