Static member variable

Source: Internet
Author: User

Unique ---- static member variable Author: HolyfireWhen learning C ++, we know the characteristics of static variables. They are not temporary variables and are generated during compilation. An example can be used 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 (); cout <" program end! "<Endl;} the result is: program start! Can you see me now! Program end! I'm go away! A A is defined only once, and the analysis structure is performed only after the main program exits. What does this mean? A is the same instance in void teststatic (void), which exists in the whole program but can only be accessed in void teststatic (void. Don't believe it? Let's try it. # 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;. INC (); cout <"count's value is:" <. count () <Endl;} void main () {cout <"program start! "<Endl; teststatic (); cout <" program end! "<Endl;} the result is: program start! Can you see me now! Count's value is: 1 // initialization count is 0, INC causes the Count auto-increment value to be 1count's value is: 2 // not initialized, INC causes the Count auto-increment value to be 2count's value is: 3 // not initialized. inc causes the Count auto-increment value to be 3 program end! I'm go away! The facts show that everything is done, so how does he implement it? In the C ++ compiler, he is created in a memory area, which is neither a heap nor a stack, the compiler will remember them during the compilation phase and assign them well so that this feature can be implemented. It looks like a global variable, but we know that using global variables will combine the coupling of modules and reduce the versatility of code, so the emergence of static member variables brings flexibility to our programming. How can we use this little item in our object-oriented programming. What role does he assume? That's exactly what I want to say. We know that the member variables of A Class represent the attributes of a class, which correspond to the material characteristics of objects. They are created when an instance of the class is created and die out when it is created. However, as mentioned above, static variables already exist during the compilation process, that is, they do not disappear as the instances are created. Is that true. Let's look at the facts. # 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 is: A is on! // When I say it again, the main program is not running, and the constructor starts to work, then the instance of B has not yet launched program start! B is on! // B1 is created, but b1.a does not create B is on! B is on! Program end! B is off! // B's instance has been destroyed, but member variable A has not destroyed B is off! B is off! A is off! // Check out. This is the destructor of A. Note the following conventions: a B: A; static member variables must be initialized outside the main function, static member variables must be initialized. In fact, B: A does not belong to B. It serves as a member of B only to determine the access permission PRIVATE: static A; because after this setting, only B can access B:, of course, we should consider it clearly during the design. If a has nothing to do with B, then this design has no practical significance. So how can we design and use this feature. Let's give an example. Sometimes we want to know how many instances of the class are instantiated several times. For example, Class A; A A1, A2, A3; should be instantiated three times. This information should be good. If you use a global variable, there are many issues to consider. The global variable will be modified at will, and the definition of the global variable and the initialization of the global variable will also cause confusion, will the global variable name be the same as other global variables. Since static variables can implement some global variables, why not try it out and see how it works. First, there is only one static member variable, which is not a real attribute of the class. In fact, there is only one variable, which does not add a burden on the class. Second, you can add access restrictions to the static member variable, as long as it is not public, it cannot be modified at will. Since there are many benefits, we can start work. # Include <iostream> using namespace STD; Class A {public: A () {count ++ ;}; // when an instance is generated, add one to the counter ~ A () {count --;} // when an instance is destroyed, the counter minus one int getinstancecount () {return count;} PRIVATE: static int count ;}; int :: count = 0; void main () {cout <"program start! "<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; Pa = new; cout <"Now creat a Class A's instance! "<Endl; cout <" number of Class A's instance is: "<a2.getinstancecount () <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;} the result is: program start! Now, have A1, A2, A3, A4, A5 instances! // There are five instances, namely A1, A2, A3, A4, and A5, number of Class A's instance is: 5 // that's right, exactly five now creat a Class A's instance! // Create one in the heap and use Pa to obtain its reference number of Class A's instance is: 6 // as expected, the number of while class's instances A4, A5, PA destroy is added! // Release one in the heap. The stack releases two only A1, A2, A3 left, is the count of instance is 3? // 6? C 1? Of course C2 is equal to 3 la number of Class A's instance is: 3 I'm right. Only when the same variable is operated in the constructor can the correct result be obtained. This technology has been applied in many aspects, such as mutex signals and reference counters like dynamic connection libraries. Remember that there is actually only one static member variable of the class, which does not increase or decrease with the creation/destruction of the class instance. It is not a real member of the class and is not part of the class.

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.