Transfer from: Staticvariable in C and C + + static data member (static member)
C language static variables:
" Span style= "color: #000000; Font-size:medium; " > 1). Static local variable
A, A static local variable is defined within a function, with a lifetime of the entire program running, but the scope is the same as an automatic variable, and can only be used within the function that defines the variable. After exiting the function, it cannot be used even though the variable continues to exist.
B, if the static local variable of the basic type is not assigned the initial value in the description, the system automatically assigns 0 values. The value of the automatic variable is indeterminate if it is not assigned the initial values.
2). Static global variable The
global variable itself is static storage, and static global variables are, of course, statically stored. But their scope, non-static global variable is the entire source program (multiple source files can be used together), while a static global variable restricts its scope, that is, only the source file (. h or. cpp, which is not available in other source files of the same source program ( Note that this is different from C + + ).
Understanding of the C language static variable:
A, if the global variable is only accessed in a single C file, you can modify the variable to a static global variable to reduce the coupling between modules;
B, if the global variable is accessed only by a single function, then the variable can be changed to the static local variable of the function, in order to reduce the coupling degree between the modules;
C, static variables and global variables are placed in the program's global data area instead of being allocated on the stack, so it is not possible to cause a stack overflow;
D, when designing and using functions that access dynamic global variables, static global variables, static local variables, you need to consider the reentrant problem (thread insecurity);
The characteristics of static data members in C + + are:
1. Static data members are not constrained by access rights only when they are initialized ;
2, static data members should not be defined in the. h file (initialization), but placed in a. cpp file defined (initialization);
3. Static data members are shared by all objects of the class, including all objects of the derived class of the class--that is, the derived class and the base class share a static member .
4, static data member type is the class itself, that is, in a class can declare the class's own type of static members of the image, but can not define ordinary member objects, (pointers can)
5. In the const member function, You can modify the value of the static member variable . The value of an ordinary member variable cannot be modified .
6. Static member functions can only access static members, cannot access non-static members, and static member functions cannot be defined as virtual, const, volatile functions .
Detailed analysis is as follows:
The declaration of a data member in the class body is preceded by the static keyword, which becomes a data member of that class.
As with other data members, static data members also adhere to public/protected/private access rules . Note: This is only a rule that does not comply with Public/protected/private when initializing.
that is, if it is a static member of private and protected, it can only be called through the static member function of public, but not directly through the class name.
static data members are actually global variables in a class domain. Therefore, is defined in the same way as a global variable. Examples are as follows:
xxx.h file
class base
{
Private
static const int _i; declares that standard C + + supports an ordered type initialization in the class body, but VC6 is not supported.
};
Xxx.cpp file
const int base::_i=10; //is not restricted by private and protected access only when defining (initializing).
Note: do not attempt to define (initialize) static data members in the header file. in most cases, doing so causes a duplicate definition of such an error . Even adding #ifndef #define #endif或者 #pragma once.
2. static data members are shared by all objects of the class . an object that includes the derived class for that class . That is, a derived class object shares a static data member of a base class with a base class object. Examples are as follows:
Class Base
{
Public:
static int _num; Statement
};
int base::_num=0; True definition of static data members
Class Derived:public Base
{
};
Main ()
{
base A;
Derived B;
a._num++;
cout<< "base class static data number _num is" <<a._num<<endl;
b._num++;
cout<< "derived class static data number _num is" <<b._num<<endl;
}
The result is 1, 2; a static data member is shared by the derived class and the base class.
3. the type of a static data member can be the type of the owning class , while a normal data member cannot . A normal data member can only be declared as a pointer or reference to the owning class type. Examples are as follows:
Class base{
Public:
Static base _object1; Correct, static data member
Base _object2;//Error
Base *pobject; Right, pointer
Base &mObject; Correct, reference (The reference needs to be initialized in the constructor's parameter initialization list, as follows:)
};
To be able to create an object, the class needs to define a constructor with a parameter initialization list, as follows:
Class base{
Public:
Static base _object1; Correct, static data member
Base *pobject; Right, pointer
Base &mObject; Correct, reference
Base (): Mobject (*this) {}
};
4. The value of a static data member can be legitimately changed in a const member function without breaking the value of that data member and cannot be changed in the const member function.
#include <iostream>
using namespace Std;
Class Student
{
Private
static int A;
int b;
Public
void change () const;
void Setb (int b);
int Getb ();
static int Geta ();
};
void student::change () const
{
a++; Span style= "font-size:medium;" > //this can, because a is a static member variable.
b++; //not, because B is an ordinary member variable (if B is not a member variable (a global variable, a normal argument, a temporary variable defined inside a function) You can also do not modify) , and is a const function
}
int Student::geta ()
{
return A;
}
void Student::setb (int b)
{
this->b = b;
}
int Student::getb ()
{
return b;
}
int student::a = 5;
int main (int argc,char *argv[])
{
Student Stu;
STU.SETB (10);
Stu.change ();
Cout<<student::geta () <<endl;
COUT<<STU.GETB () <<endl;
return 0;
}
That is: The const constraint of the member function only works on the ordinary member variables of the class , and for global variables, member function parameters, the temporary variables inside the member functions do not work.
Go: Static variables and C + + static data members in C language (static member)