[C/C ++] Static class member in C ++

Source: Internet
Author: User

Before writingProgramThis is often the case where you need to count the number of all object instances of a class. A simple method is to use a global variable, but its disadvantages are also obvious. Using static members can solve this problem well. Compared with using global variables to use static members, it has the following advantages:

1) Static members are in the scope of the class, which effectively avoids the problem of duplicate names with other global variables.

2) It can be encapsulated. Static members can be private in access control, just like other members.

2) It is easy to see the meaning of the variable and its association with the class. Is a relatively goodCodeStyle.

 

Defining and using static members in C ++ is significantly different from defining and using static members in Java and C. I have encountered many errors. Here we will summarize the usage:

1. Description and definition of static members

The declaration of static members is in the class definition body.

Example:

1ClassTestclass2 {3Private:4Static IntItemcount;5Static IntInititemcount ();6};

 

If you call the static member only in this way, the undefined error will occur. The definition of the static member must be external to the class definition:

1 IntTestclass: itemcount = inititemcount ();

You do not need to add the static keyword in the definition. The definition of static member functions is the same as that of member variables.

Note that you Cannot initialize static member variables.

 

Ii. use static members

You can directly use static members within the class:

 1   Class  Testclass  2   {  3          Public  :  4   Testclass ();
~ Testclass (); 5 Static Int Getitemcount (); 6 Private : 7 Static Int Itemcount; 8 Static Int Inititemcount (); 9 };
 1  Testclass: testclass ()  2   {  3 Itemcount ++ ;  4   }  5   6   Int Testclass: itemcount = Inititemcount ();  7   8   Int  Testclass: inititemcount ()  9  {  10        Return   0  ;  11   }  12   13 Testclass ::~ Testclass ()  14   {  15 Itemcount -- ;  16 }

Call static members outside the class:

 1 # Include <iostream> 2 # Include "testclass. H" 3   Int  Main ()  4   {  5   Testclass T;  6 Testclass * PT = & T;  7   T. inititemcount ();  8 Pt-> Inititemcount ();  9  Testclass: inititemcount ();  10        Return   0  ;  11 }

3. Const static member variables

The const static member variable must be initialized while being declared.

 

 

Related Article

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.