Introduction to static keywords in C ++

Source: Internet
Author: User
Preface:
This article is just a Summary of the static type in C ++. if the error is correct, please help me correct it. I will summarize it in two aspects. The first aspect is mainly relative to the process orientation, that is, the class is not involved in this aspect, and the second aspect is relative to the object-oriented, it mainly describes the role of static in the class.

I. Static keywords in process-oriented design

1. Static global variables

Definition: Before a global variable, add the keyword "static". The variable is defined as a static global variable.

Features:
A. The variable allocates memory in the global data zone.
B. Initialization: If Explicit initialization is not performed, the value 0 is implicitly initialized.
C. The access variable is only visible in the source file. Strictly speaking, it should be defined from the beginning to the end of this file.

Example (from the C ++ Programming Tutorial --- Qian Neng, editor of p103 ):
// File1.cpp
# Include <iostream. h>
Void FN ();
Extern int N;
Void main ()
{
N = 20;
Cout <n <Endl;
FN ();
}

// File2.cpp
# Include <iostream. h>

Static int N; // defines the static global variable. The initialization value is 0;
Void FN ()
{
N ++;
Cout <n <Endl;
}

The file can be compiled separately, but the Variable N in file1.cpp cannot be defined during the connection, resulting in a connection error.

D. The constant declared in the file scope is static storage by default.

2. Static local variables

Definition: When the static keyword is added before a local variable, the static local variable is defined.

Features:
A. The variable allocates memory in the global data zone.
B. Initialization: If Explicit initialization is not performed, the value 0 is implicitly initialized.
C. It always resides in the global data zone until the program is running. But its scope is local scope. When the function or statement block defining it ends, its scope ends.

3. Static functions (note the differences between static member functions and classes)

Definition: add the static keyword before the return type of the function. The function is defined as a static function.

Features:
A. Static functions can only be used in the source file (which is different from common functions)
Example (from the C ++ Programming Tutorial --- Qian Neng, editor of p103 ):
// File1.cpp
Void FN ();
Void staticfn ()

Void main ()
{

FN ();
Staticfn ();
}

// File2.cpp
# Include <iostream. h>

Static void staticfn ();
Void FN ();

Void FN ()
{
Staticfn ();
Cout <"this is FN ()/n ";
}

Void staticfn ()
{
Cout <"this is staticfn ()/n ";
}
The staticfn () function cannot be found during connection.

B. Ideas

The Inline Function declared in the file scope is static by default.

2. Static keywords in a face object (mainly static keywords in the class)

1. static data member

Features:
A. Memory Allocation: it is allocated in the global data zone of the program.
B. Initialization and definition:
A. Space should be allocated when defining static data members, so they cannot be defined in class declaration.
B. In order to avoid repeated definitions in multiple source files using this class, it cannot be in the header file of the class.
Definition.
C. static data members must exist at the beginning of the program running, so their initialization is best implemented within the class.
C. Features
A. The influence of the public, protected, and private keywords is the same as that of common data members,
B. Because its space is allocated in the global data zone and all objects of this class are shared, it is not a specific class object and its scope is visible when no class object is generated, that is, when no class instance is generated, we can operate on it.

D. Access Form
A. Class Object Name. static data member name
B. class type name: static data member name

E. static data members are mainly used for attributes owned by all instances of the class. For example, for a deposit type, the account is different from each instance, but the interest of each instance is the same. Therefore, the interest should be set as the static data member of the deposit class. There are two advantages: first, no matter how many deposit-type objects are defined, interest data members share the memory allocated in the global zone, thus saving storage space. Second, once the interest needs to change, the interest of all deposit objects will change once, because they actually share one thing.

2. static member functions

Features:
A. static member functions are associated with classes, but not with class objects.
B. static member functions cannot access non-static data members. The reason is simple. Non-static data members belong to specific class instances.

Purpose:
It is mainly used for operations on static data members.
Call form:
A. Class Object Name. static member function name ()
B. class type name: static member function name ()
 

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.