unique
---- Static member variable
Author: HolyFire
When we learn C , we know the characteristics of static variables. He is not a temporary variable, which has been produced during compilation. Use an example to explain the problem.
#include
Using namespace std;
Class a {
PUBLIC:
A () {cout << "can you see me now!" << endl;}
~ A () {cout << "I'm Go Away!" << endl;}
}
Void TestStatic (Void)
{
Static a a;
}
void main ()
{
Cout << "Program Start! << endl;
TestStatic ();
TestStatic ();
TestStatic ();
COUT << "Program end!" << endl;
}
turn out:
Program start!
Can you see me now!
PROGRAM END!
I'm Go Away!
A a is only defined once, and the designer is only available after the main program exits. What did this explain, a a is the same instance in Void TestStatic (Void), which exists throughout the program but only in Void TestStatic (Void) can access him.
Do not believe? Then let's try it.
#include
Using namespace std;
Class a {
Private:
INT country;
PUBLIC:
A (): count (0) {cout << "can you see me now!" << endl;}
~ A () {cout << "I'm Go Away!" << endl;}
Void inc () {count ;}
INT count () {return count;}
}
Void TestStatic (Void)
{
Static a a;
a.inc ();
COUT << "count's value is:" << a.count () << endl;
}
void main ()
{
Cout << "Program Start! << endl;
TestStatic ();
TestStatic ();
TestStatic ();
COUT << "Program end!" << endl;
}
turn out:
Program start!
Can you see me now!
Count's value is: 1 // Initialization Count is 0, incnation causes COUNT self-value value should be 1
Count's value is: 2 // No initialization, INC causes COUNT self-value value to be 2
Count's value is: 3 // No initialization, INC causes COUNT self-value value to be 3
Program end!
I'm Go Away!
The facts explain everything, then how he is implemented, the C compiler, he was created in a memory area, this area is not a stack, the compiler remembers them in the compilation phase, and for them Do a good job in allocation, so you can achieve this feature. It seems that his role is like global variables, but we know that the use of global variables will add the coupling of the module, reducing the versatility of the code, so static member variables have the flexibility of our programming.
How to use this little thing in our object-oriented programming. What kind of role he plays, this is what I want to say.
We know that the member variable of the class represents a class of properties, corresponding to the material characteristics of the object, and they are created when they are created in an instance of the class, and die when dying. However, above said that static variables already exist during compilation, that is, if they are created when they are created, die when dying. Is that right. Look at the facts.
#include
Using namespace std;
Class a {
PUBLIC:
A () {cout << "a is on!" << endl;
~ A () {cout << "a is off!" << endl;}
}
Class b {
PUBLIC:
B () {cout << "b is on!" << endl;
~ B () {cout << "b IS OFF!" << endl;}
Private:
Static a a;
}
A b :: a;
void main ()
{
Cout << "Program Start! << endl;
B B1, B2, B3;
COUT << "Program end!" << endl;
}
turn out:
A is on! // I said again, the main program has not yet been running, the constructor started, and the instance of B has not debuted.
Program start!
B is on! // B1 created, but B1.A did not create
B IS on!
B IS on!
Program end!
B is off! // b instance destroyed, but member variables A did not destroy
B IS off!
B IS off!
A is off! // look, this is the destructor of A
Pay attention to a convention:
A b :: a;
The initialization of static member variables must be outside the main function, and the static member variable must initialize.
In fact, B:: A does not belong to B, and the members of him are just to determine the permissions of access.
Private:
Static a a;
Because this is set to access B: A, it is clear when it is designed. If A and B have no relationship, then such a design does not have any actual meaning.
So how to design this feature can be used.
Let us give an example.
We sometimes want to know that there are several examples, which is also instantiated several times. such as
Class A;
A A1, A2, A3;
Then it should be instantiated 3 times. Can know this information is very good. If you use a global variable, then there are many problems that should be considered. The global variable will be arbitrarily modified. The global variable is defined there and the global variable initialization will also bring confusion. The global variable will not be renamed with other global variables. and many more.
Since static variables can realize the function of some global variables, why not cut the knife, look at how the effect. First, the static member variable has only one, and is not the true attribute of the class. It is actually only one variable, which does not increase the burden of the class; the second can give him an access restriction, as long as it is not public, then you can't modify it. Since there are a lot, then start. #include
Using namespace std;
Class a {
PUBLIC:
A () {count ;}; // When an instance is generated, the counter adds one
~ A () {count-;} // When destroying an instance, the counter minus one
INT GetInstanceCount () {return count;}
Private:
Static int count;
}
INT a :: count = 0;
void main ()
{
COUT << "Program Start! << Endl << endl;
A A1, A2, A3;
{
A A4, A5, * Pa;
Cout << "NOW, HAVE A1, A2, A3, A4, A5 Instances!" << endl;
Cout << "Number of class a's instance is:" << A1.GetInstanceCount () << endl << endl;
PA = new a;
COUT << "Now Creat A class a's instance!" << endl;
Cout << "Number of class a's instance is:" << A2.GetInstanceCount () << endl << endl;
DELETE PA;
}
COUT << "While Class's Instances A4, A5, Pa Destroy!" << endl;
COUT << "Only A1, A2, A3 LEFT, IS THE COUNT OF INSTANCE IS 3?" << Endl;
COUT << "Number of class a's instance is:" << a3.GetInstanceCount () << endl << endl;
}
turn out:
Program start!
NOW, HAVE A1, A2, A3, A4, A5 INSTANCES! / / Five instances of A1, A2, A3, A4, A5
Number of class a's instance is: 5 // is right, it is five
Now create! // created one in the pile, using PA to get his reference
Number of class a's instance is: 6 // The same is the same, the number increases
While Class's Instances A4, A5, Pa Destroy! // Releases one in the stack, 2
ONLY A1, A2, A3 Left, IS The Count of Instance IS 3? // 6 - 1 -2 is of course equal to 3
Number of class a's instance is: 3 I am not wrong. Since the same variable is operated in the constructor, the correct result can be obtained. This technology is applied in many ways, such as mutually exclusive signals, like dynamic connection libraries, and the like.
What you have to remember is that the static member variables of the class actually have only one, it does not increase / decrease as the class's instance is created / destroyed. It is not a true member of the class, is not part of the class.
2001/9/7
Ding Ning