Local static variables of a class member function

Source: Internet
Author: User

I encountered a problem in my previous work, as described in the topic. It seems that the problem is complicated. Here I will explain that the requirements encountered at that time should be handled as follows: when calling a member function of a class object, the first time there is a specific significance, other times are unchanged, meaningless. This requirement can be seen as the first initialization when a member function is called, and other operations are not performed, that is, initialization is performed during the first call. Based on this, it is easy to think of the static variable in c/c ++. Its function is to keep the variable content persistent, variables stored in the static data area will be initialized at the beginning of the program, which is also the only initialization. Use static local variables as needed and write the following code:

1 class T 2 {3 public: 4 T (const char * pstr): value (pstr) {}5 void Print () const; 6 private: 7 string value; 8 }; 9 10 void T: Print () const11 {12 static bool bFirstCall = true; 13 if (bFirstCall) 14 {15 cout <"first Call" <value <endl; 16 bFirstCall = false; 17} 18 else19 {20 cout <"not first Call" <value <endl; 21} 22}

That is, define a local static variable in the member function of the class, so that the first call is initialized to identify whether it is the first call, and then run the else branch. Run the following test code. The output result is as follows:

1 int _ tmain (int argc, _ TCHAR * argv []) 2 {3 T t1 ("Grubby"); 4 t1.Print (); 5 t1.Print (); 6 7 T t2 ("Moon"); 8 t2.Print (); 9 t2.Print (); 10 11 return 0; 12}

The output shows that t1 gets the desired result, but t2.Print () prints "not first Moon" twice. This indicates that bFirstCall is false at this time. Print the bFirstCall address code in the Print function:

1 void T: Print () const 2 {3 static bool bFirstCall = true; 4 printf ("addr of bFirstCall is % x \ n", & bFirstCall ); 5 if (bFirstCall) 6 {7 cout <"first Call" <value <endl; 8 bFirstCall = false; 9} 10 else11 {12 cout <"not first Call" <value <endl; 13} 14}

Run the following output again:

The bFirstCall address printed when the Print function is called four times is the same, which means that the local static variables in the member functions of the class also belong to this function, rather than an object, does not redefine a t2 object. When t2.Print () is called for the first time, bFristCall is true. No matter which T class object is called, bFirstCall = true only when Print () is called for the first time. So I had no choice but to modify the program and defined bFristCall as a static member of the class. Each time the constructor resets it to true:

1 class T 2 {3 public: 4 T (const char * pstr): value (pstr) {bFirstCall = true;} 5 void Print () const; 6 private: 7 string value; 8 static bool bFirstCall; 9}; 10 bool T: bFirstCall; 11 12 void T: Print () const13 {14 if (bFirstCall) 15 {16 cout <"first Call" <value <endl; 17 bFirstCall = false; 18} 19 else20 {21 cout <"not first Call" <value <endl; 22} 23}

Run the test code to get the desired result:

However, a new problem occurs here, that is, every time the constructor resets bFirstCall to true, if a new object is defined and the Print () function is not called, then, calling the Print () function of the previously defined object will produce the opposite result as expected. Consider the following test code:

1 int _ tmain (int argc, _ TCHAR * argv []) 2 {3 T t1 ("Grubby"); 4 t1.Print (); 5 t1.Print (); 6 7 T t2 ("Moon"); 8 t1.Print (); 9 10 return 0; 11}

The output is as follows:

To avoid this situation, we can only initialize and call a function similar to Print for each object in sequence, but we have not yet come up with a good solution.

Local static variables of a class member function

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.