Static member variables

Source: Internet
Author: User

Unique----static member variables HolyfireWhen we learn C + +, we know the nature of static variables, which are not temporary variables that have been produced during compilation. Use an example to illustrate the problem. #include <iostream>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;} void Main () {        cout << "program start!" << endl;         teststatic ();        teststatic ();         teststatic ();        cout << "program end!" << Endl;} The result: Program start! Can you See me now! Program end! I ' m Go away! A A is defined only once, and the destruction is done after the main program exits. What does this mean, a A in void teststatic (void) is the same instance, he exists throughout the program but only in void teststatic (void) can access him. Don't believe it? Let's try it out. #include <iostream>using namespace Std;class a{private:        int count;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;} The result: Program Start! Can you see me now!count ' s value is:1   //initialization count for 0,inc causes the Count self-bonus to be 1count ' s value is:2  &nb sp;//is not initialized, Inc causes the Count self-bonus value to be 2count ' s value is:3   //not initialized, Inc. causes the Count self-bonus to be 3Program end! I ' m Go away! facts Explain everything, then how did he do it, in the C + + compiler, he was created in a memory area, this area is not a heap or stack, the compiler will remember them at the compile stage, and do the assignment for them, so that you can implement this feature. It seems that his role is somewhat like a global variable, but we know that the use of global variables will add to the coupling of the module and reduce the generality of the code, so the emergence of static member variables for our programming flexibility. How to use this little thing in our object-oriented programming. What kind of role does he play, and that's what I'm going to say. We know that the member variables of a class represent the properties of a class, corresponding to the object's physical properties, which are created when an instance of the class is created, and perish when it dies. But it says that static variables exist during compilation, which is not created when the instance is created, and dies when it dies. Is that so? Look at the facts and speak. #include <iostream>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 b::a;void Main () {        cout << "program start!" << endl;         B b1,b2,b3;        cout << Program end! "<< Endl;} The result: A is on! //look, I said again, the main program has not yet run, the constructor has started to work, then the instance of B has not yet appeared program start! B is On! //b1 was created, but b1.a did not create a B is on! B is on! Program end! The instance of B is Off! //b was destroyed, but member variable a did not destroy B is off! B is off! A is off!  //see, this is a destructor. Note A convention: a b::a; the initialization of static member variables must be outside the main function, and static member variables must be initialized. In fact b::a does not belong to B, and he is a member of B only in order to determine the access permissions private:        static A; Because this is set to only B to access b::a , of course, the design should be considered clearly, if a and B are not related, then such a design has no practical significance. So how to design to use this feature. Let's give an example. There are times when we want to know how many instances of a class have been instantiated several times. such as Class A; a a1,a2,a3; then it should be instantiated 3 of times. It should be nice to know this information. If you use a global variable, there are many issues to consider, global variables are arbitrarily modified, global variables are defined there and global variables are initialized there will be confusion, global variables will not be the same as other global variables and so on. Since static variables can implement some of the functions of global variables, why not kind to see how the effect works. First static member variable only one, and not the real property of the class, actually only one variable, does not increase the burden of the class;To give him access restrictions, as long as it is not public then can not be arbitrarily modified. Since the benefits are so great, start working. #include <iostream>using namespace Std;class a{public:       A () {count++;};  //when an instance is generated, the counter adds a     ~a () {count--;}  //when destroying an instance counter minus one        int getinstancecount () {return count;} private:       static int count;}; int a::count = 0;void Main () {       cout << program start! << Endl << E ndl;       A a1,a2,a3;       {               A a4,a5,*pa;               cout << "Now, with A1, A2, A3, A4, A5 instances!" << endl; &n bsp;            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, was the Count of Instance is 3? "<< endl;       cout <<" number of class A ' s Instance is: "<< A3. Getinstancecount () << Endl << Endl;} The result: Program start! Now, with A1, A2, A3, A4, A5instances! //A1, A2, A3, A4, A5 five instances number of class A ' s Instance is:5      //Yes, it's five. Now creat a Class A ' s instance!        //created a in the heap, using PA to get his reference to number of Class A ' S Instance is:6      //as much as thought, the number increased while class ' s Instances A4, A5, PA destroy! //in the heap Free one, stack free 2 only A1, A2, A3 left, are the Count of Instance is 3? &NBSP;//6? C 1? c2  of course equals 3. Number of class A ' s Instance is:3  I'm right about that. Because the same variable is manipulated in the constructor, the correct result can be obtained. This technique is used in many ways, such as mutex signals, reference counters like dynamic connection libraries, and so on. It is to be remembered that the static member variable of a class is actually only one, which does not increase/decrease with the creation/destruction of instances of the class. It is not a true member of a class, it is not part of a class.  http://blog.csdn.net/adcxf/article/details/2106555

Static member variable

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.