The static of C + +

Source: Internet
Author: User

the static of C + + has two usages: static in process-oriented programming and static in object-oriented programming. The former applies to ordinary variables and functions, and does not involve classes; the latter mainly describes the role of static in classes.
1. Static for process design
1.1 Static global variables
    • A static global variable is formed by the description of the global variable (external variable), preceded by static.
    • Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.
    • Static global variables are initialized only once.
1.2. Static local variables
before a local variable, the variable is defined as a static local variable, plus the keyword static. Static Local variables have the following characteristics:
  • the variable allocates memory in the global data area;  
  • A static local variable is first initialized when the program executes to the declaration of the object, that is, the subsequent function call is no longer initialized;  
  • static local variables are typically initialized at the declaration and, if not explicitly initialized, are automatically initialized to 0 by the program;  
  • It always resides in the global data area until the program finishes running. But its scope is local scope, when the function or statement block that defines it ends, its scope ends;
1.3 static functions
The definition and declaration of a function is extern by default and is visible in other files.
The function is defined as a static function by adding the static keyword before the return type of the function. A static function differs from a normal function in that it can only be seen in the file that declares it and cannot be used by other files.
Benefits of defining static functions:
    • static functions cannot be used by other files;  
    • Other files can define a function of the same name, and no conflict will occur;
Second, the object-oriented static keyword (the static keyword in the Class)
2.1 static data members
precede the declaration of a data member within a class with the keyword static, which is a static data member within the class.
As you can see, static data members have the following characteristics:
  • for non-static data members, each class object has its own copy. Static data members are treated as members of the class. Regardless of how many objects of this class are defined, static data members have only one copy in the program and are shared by all objects of that type. In other words, a static data member is common to all objects of that class. For multiple objects of this class, static data members are allocated only once for all objects to be shared. Therefore, the value of the static data member is the same for each object, and its value can be updated;
  • Static data members are stored in the global data area. Static data members are defined when they are allocated space, so they cannot be defined in a class declaration.
  • static data members comply with PUBLIC,PROTECTED,PRIVATE access rules like normal data members;  
  • because static data members allocate memory in the global data area, all object shares of this class are shared, so it does not belong to a particular class object, its scope is visible when no class object is produced, that is, we can manipulate it without producing an instance of the class;  
  • static data member initialization differs from general data member initialization. The static data member is initialized in the format:data type >< class name >::< static data member name >=< value
  • the static data members of a class are accessed in two ways: class object name >.< static data member name or class type name >::< static data member name
  • If the access permission of the static data member is allowed (that is, the member of public), the static data member can be referenced in the program according to the above format;   
 
1.  static data members do not enter the program's global namespace, so there is no possibility of conflicts with other global names in the program;   2.  can implement information hiding. Static data members can be private members, while global variables cannot;
2.2 static member functions As with static data members, we can also create a static member function that serves the entire service of a class rather than a specific object of a class. Static member functions, like static data members, are internal implementations of the class and are part of the class definition. Ordinary member functions generally imply a this pointer, which points to the object of the class itself, because ordinary member functions are always specific to the specific object of a class. Typically, this is the default. such as the function fn () is actually THIS->FN (). However, compared to a normal function, a static member function does not have the this pointer because it is not associated with any object. In this sense, it cannot access non-static data members that belong to a class object, nor can it access a non-static member function, which only calls the rest of the static member functions .

For static member functions, you can summarize the following points:

? A function definition that appears outside the class body cannot specify a keyword static;

? Static members can be accessed from one another, including static member functions that access static data members and access static member functions;

? Static member functions and static data members can be accessed arbitrarily by non-static member functions;

? Static member functions cannot access non-static member functions and non-static data members;

? Because there is no additional overhead for this pointer, the static member function has a slight increase in speed compared to the global function of the class;

? Call a static member function, which can be accessed with the member access operator (.) and (-) call a static member function for an object of a class or pointer to a class object, or you can use the following format directly:

The class name >::< static member function name, or parameter table, to invoke the static member function of the class.

 
 Static functions of C language

When a source program consists of multiple source files, the function is divided into internal functions and external functions, depending on whether the function can be called by functions in other source files.
1 intrinsic functions (also called static functions)
If a function defined in a source file can only be called by a function in this file, but not by a function in the other file of the same program, this function is called an intrinsic function.
Define an intrinsic function by simply adding a "static" keyword before the function type:
static function type function name (function parameter table)
The keyword "static", translated into Chinese, is "still", so the internal function is also called the static function. But the meaning of "static" here does not mean storage, but the scope of the function is limited to this file.
The advantage of using intrinsic functions is that when different people write different functions, they don't have to worry about the functions they define, or whether they have the same name as the functions in other files, because the same name doesn't matter.

2 external functions
Definition of an external function: when defining a function, if the keyword "static" is not added, or the keyword "extern", it means that the function is an external function, which is our most commonly used form of function.


  • Static global variable: Change the scope without changing the storage location
  • Static local variable: Change the storage location without changing the scope
  • Similarly, for a function within a class, plus static does not change its storage location (all in the code snippet), but changes its scope, it is necessary to create the class object before calling the member function, you can now call the member function directly with the class name without creating the class object.
  • Java's main function is written in the class, the program in the process of execution, the first to invoke the main function, but the class containing the main function is not created object and then cannot call the main function, so the main function must be static type. For C + +, its main function is the default type , which is the extern type, which can be accessed in all files and can be accessed directly during the execution of the program.














From for notes (Wiz)

The static of C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.