Static data member in C ++

Source: Internet
Author: User

In the C ++ development environment, static member functions, like static data members, all belong to static members of the class and are not object members. Therefore, the reference to static members does not need to use the object name, but references non-static members.

Static Data Member,

In a class, static members can share data between multiple objects. Using static data members does not undermine the hidden principle, which ensures security. Therefore, static members are shared among all objects of the class, rather than members of an object.

Static data members can save memory because they are public to all objects. Therefore, for multiple objects, static data members can only store one object for sharing. The value of a static data member is the same for each object, but its value can be updated. You only need to update the value of the static data member once to ensure that all objects have the same value after the update, which improves the time efficiency.

The usage and precautions of static data members are as follows:

1. When defining or describing static data members, add the keyword static.

2. static member Initialization is different from general data member initialization. The format of static data member Initialization is as follows:

<Data type> <class name >:< static data member name >=< value>

This indicates:

(1) initialization is performed in the external class without static before, so as to avoid confusion with general static variables or objects.

(2) during initialization, the access control letter private and public of the member are not added.

(3) during initialization, the scope operator is used to indicate the class to which it belongs. Therefore, static data members are members of the class, not members of the object.

3. static data members are stored in static mode, which indicates the lifetime of static data and must be initialized.

4. When referencing static data members, use the following format:

<Class name >:: <static member name>

If the access permission of the static data member is allowed (that is, the public Member), you can reference the static data member in the program in the preceding format.

The following example illustrates the application of the C ++ static data member:

 
 
  1. #include   
  2. class Myclass  
  3. {  
  4. public:  
  5. Myclass(int a, int b, int c);  
  6. void GetNumber();  
  7. void GetSum();  
  8. private:  
  9. int A, B, C;  
  10. static int Sum;  
  11. };  
  12.  
  13. int Myclass::Sum = 0;  
  14.  
  15. Myclass::Myclass(int a, int b, int c)  
  16. {  
  17. A = a;  
  18. B = b;  
  19. C = c;  
  20. Sum += A+B+C;  
  21. }  
  22.  
  23. void Myclass::GetNumber()  
  24. {  
  25. cout<<"Number="<<<","<<<","<< 
  26. }  
  27.  
  28. void Myclass::GetSum()  
  29. {  
  30. cout<<"Sum="<< 
  31. }  
  32.  
  33. void main()  
  34. {  
  35. Myclass M(3, 7, 10),N(14, 9, 11);  
  36. M.GetNumber();  
  37. N.GetNumber();  
  38. M.GetSum();  
  39. N.GetSum();  

The output result shows that the Sum value is equal to the M object and N object. This is because when the M object is initialized, the Sum of the values of the three int-type data members of the M object is assigned to Sum, so Sum saves the value. When initializing the N object, Sum the values of the three int-type data members of the N object and add them to the existing values of Sum. Therefore, Sum will save the other values. Therefore, the values referenced by object M and object N are the same.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.