Summary of C/C ++ static usage

Source: Internet
Author: User
Tags variable scope

Summary of C/C ++ static usage
Zookeeper

Static usage
A. Static local variables become static local variables (with memory and global storage permissions)
B. Static global variables (restrict the corresponding global variables to be called by other files)
C. Static Functions
D. Static class member (identifies this member as a class rather than a specific object)

1. Static local variables
1.1 static local variables are defined in the function and have a static storage period instead of an automatic storage period, because variables with a static storage period have a permanent

Storage unit, so the value of the variable is retained throughout the program storage. Although the variable still exists after exiting the function, it cannot be used.


1.2 static local variables always have block scopes. (Same as automatic variable scope)


1.3 if an initial value is assigned to a static local variable of the basic type, the system automatically assigns the value 0.


1.4 example:
Void static_local_variable ()
{
Static int count = 0;
Count ++;
}


The first time you enter this function, the static variable count is initialized to 0 (if not initialized, the system will automatically initialize to 0), and then execute count ++.

However, if you call this function later, only count ++ is executed.


This function implements the same functionality as the following code:
Int count = 0;
Void fuc ()
{
Count ++;
}


2. Static global variables
2.1 static storage period. As declared as static local variables, external variables have a static storage period and are stored in external variables.

It is permanently retained.


2.2 File Scope. The external variable has the file scope, starting from the declared position of the variable to the end of the file. Therefore

All functions declared after external variables can be accessed.


2.3 example:
// Xxxx. cpp
Void fucOne ()
{
SCount ++;
Count ++;
}

Static int sCount = 0;
Int count = 0;

Void fucTwo ()
{
SCount ++;
Count ++;
}

A. fucOne () is defined before the sCount and count variables. during compilation, no identifier is declared. FucTwo () is defined after two variables

FucTwo () can be used normally. It is not accessible in other. cpp files.


B. If the two. cpp files declare global variables with the same name, they are essentially two independent static global variables.


The c. count variable can be shared, and extern int count is declared in the xxx. h file. The global variable can be shared once initialized in any. cpp file that contains this header file. The same method is not applicable to static variables.
(Strongly recommended: do not define static or static variables in. H files)

3. static Functions
3.1 Internal functions (static functions, which cannot be called in other files)
If the function defined in an original file can only be used by this source file, add the static keyword before the function type. (Used

Static File Scope)


Example:
Static void fuc ()
{
}


3.2 External functions (which can be called in other files)

Example:

// File. cpp
[Extern] void fuc ()
{
}

// Main. cpp
Int main ()
{
Extern void fuc ();
}

4. Static class members
The static members of the 4.1 class can be private or public. Static data members can be constants, pointers, references, and class types.
4.2 its static members belong to a class rather than a specific object. The this pointer cannot be used in static member functions.
4.2 Use of static members
A. Define and initialize
Generally, static data members cannot be initialized within the class. On the contrary, each static data member must be defined and initialized externally.

Member.


Example:
// Xxxx. h
Class
{
Private:
Static int count; // Declaration
Public:
Static void fuc ();
}

Int A: count = 0; // define and initialize
(Note: it is best to initialize the definition of static data members in the corresponding. cpp file. If the header file is included multiple times as defined above, the static members will be repeatedly defined)


B. class initialization
If the application scenario of a data member is limited to situations where the compiler can replace it, an initialized const or constexpr static does not need to be defined separately.
Example:

Class Account
{
Public:
Static doule fuc (){};
Private:
Static constexpr int NUM = 30;
Int array [NUM];
}

If it is used in a scenario where the value cannot be replaced, the member must have a Definition Statement:
Example:
Constexpr int Account: NUM; // only defined, not initialized. Initialization is provided in the class
(Suggestion: even if a constant static data member is initialized in the class, the member should be defined outside the class normally)

C. Two applications of static members
(1) Static members can be incomplete types (incomplete types: declared only, undefined)
(2) Static members can be used as default real parameters.

Related Article

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.