I. Ensuring the initialization of variables
Define a variable, int x;
In some contexts x is initialized to 0, but in other contexts it is not guaranteed.
Class CPoint
{
int M_ix;
int M_iy;
}
CPoint pt;//statement a dot PT
The member variables of PT are sometimes initialized to 0, sometimes not.
Reading an uninitialized object results in an indeterminate behavior. In some cases, reading an uninitialized object will cause your program to terminate, but in some other cases it will read in some random bits. Eventually leads to unpredictable procedural behavior.
The best way to do this is to always initialize the object before it is used, and for a built-in data type without any members, you must manually complete the initialization. objects as well, to ensure that each object initializes each member of the object at construction time.
Initializing a list is more efficient than assigning values.
Differences in assignment and list initialization:
1. When initializing an object variable with an assignment, the default constructor initialization is called before the constructor executes, and the assignment operation is performed immediately. So everything that the default constructor does is wasted. By initializing the list practice, you avoid duplication of operations.
2. In some cases, the initialization list is used even if the assignment and initialization lists are both efficient. If member variables are const or reference, they must be initialized and cannot be assigned a value.
3. Because C + + has a fixed initialization order, the base class is initialized before the subclass, variables in class are always initialized in the order in which the variables are declared, independent of the order of member initialization lists, so it is best to order the declarations in the declaration order when initializing the variables in the member initialization list.
Two. Local variables and global variables
Variables typically contain 4 kinds: Global variables, static global variables, static local variables, local variables.
A global variable is also known as an external variable, which is a variable defined outside the function. It does not belong to which function, but belongs to a source program file.
Global variable Description:
1. The external variable definition must be outside of all functions and can only be defined once.
2. The external variable indicates that the function that is now to use the external variable may appear multiple times throughout the program. The general form is the extern type specifier variable name
3. When the external variable is defined, the memory unit is allocated, the external variable definition can be initially assigned, and the external variable description cannot be assigned the initial value, only indicates that an external variable is to be used within the function
4. External variables can enhance the data connection between function modules, but make the function dependent on these variables, thus reducing the independence of the function. This is detrimental from the point of view of modular programming, so try not to use global variables when unnecessary
5. In the same source file, global variables and local variables are allowed to have the same name. Global variables do not work within the scope of local variables.
A static storage variable is usually allocated at the time the variable is defined and remains unchanged until the end of the entire program.
From the scope view:
Global variables have global scope. A global variable can be used for all source files simply by defining it in one source file. Additional source files are only required by the extern keyword declaration
A static global variable also has a global scope, which is used only in the file that defines it, and cannot be applied to other files, that is, it has a file scope.
A static local variable has a local scope, which is initialized only once, since it was initialized for the first time until the end of the program run, but only visible to the body of the function that defines itself
Local variables are also only local scope, it only exists during function execution, once the function's call execution is finished, the variable is revoked, and the memory occupied is also retracted.
From allocating memory space to see
Global variables, static global variables, static local variables allocate space in static stores, and local variables allocate space in stacks
C + + variables