C + + static data members and static member functions

Source: Internet
Author: User

in general, if there are N homogeneous objects, each object has its own data member, and the data members of different objects have their own values, which are irrelevant to each other. But sometimes people want to have one or several data members that are common to all objects, which enables data sharing.

You can use global variables to achieve the purpose of sharing data. For example, in a program file has multiple functions, each function can change the value of global variables, the value of the global variable for each function sharing. However, the security of global variables is not guaranteed, because the value of global variables can be freely modified everywhere, it is possible to accidentally mistake, the value of the global variable is modified, resulting in the failure of the program. Therefore, global variables are seldom used in actual work.

You can use static data members if you want to share data between multiple objects of the same type, and do not use global objects. static data membersA static data member is a special kind of data member. It starts with the keyword static. For example
  
 
  1. class Box
  2. {
  3. Public :
  4. int Volume ( );
  5. Private :
  6. Static int height; //define the height as a static data member
  7. int width;
  8. int length;
  9. };
If you want the value of height in each object to be the same, you can define it as a static data member so that it is common to each object, not just the members of an object, and all objects can reference it.

A static data member occupies only one copy of the memory space. Each object can refer to this static data member. The value of a static data member is the same for all objects. If you change its value, the value of the data member in each object changes at the same time. This will save space and improve efficiency.

A few notes about static data members:
1) If only the class is declared and no object is defined, the generic data member of the class is not a memory space, and the data members of the object are allocated space only when the object is defined. Static data members, however, do not belong to an object and do not include the space occupied by static data members in the space allocated for the object. A static data member is a separate space outside of all objects. Whenever a static data member is defined in a class, it can be referenced even if the object is not defined and the static data member is allocated space.

You can have one or more static data members in a class, and all objects that share these static data members can reference it.

2) for a static variable, if a static variable is defined in a function, the static variable is not released at the end of the function, and the value is still present and preserved. A static data member is similar, it does not allocate space with the object's creation, nor is it released with the object's revocation (the general data member allocates space at the time the object is established and is disposed when the object is revoked). A static data member is allocated space at the time the program compiles, freeing space at the end of the program.

3) static data members can be initialized, but can only be initialized outside of the class body. Such as
int box::height=10; Represents the initialization of a data member in a box class.
The general form is:
Data type class Name:: static data member name = initial value;
You do not have to add static to the initialization statement.

Note that you cannot initialize a static data member with a parameter. Defining a constructor like this in the definition box class is an error:
Box (int h,int w,int len): Height (h) {}//error, height is a static data member

If a static data member is not assigned an initial value, the compilation system automatically assigns an initial value of 0.

4) A static data member can be referenced either by an object name or by a class name. Please observe the following procedure.

[Example 9.10] refers to a static data member.
  
 
  1. #include <iostream>
  2. using namespace std;
  3. class Box
  4. {
  5. Public :
  6. Box (int,int);
  7. int Volume ( );
  8. Static int height; //define height as a public static data member
  9. int width;
  10. int length;
  11. };
  12. Box::box(int w,int len) // Assigning an initial value to width and length through a constructor function
  13. {
  14. Width=W;
  15. Length=len;
  16. }
  17. int Box::Volume( )
  18. {
  19. return (height*width*length);
  20. }
  21. int Box::height=ten; //For static data member height initialization
  22. int Main ( )
  23. {
  24. Box a(a),b( , - );
  25. cout<<a. Height<<endl; //referencing static data members by object name a
  26. cout<<b. Height<<endl; //referencing static data members by object name B
  27. cout<<Box::height<<endl; //referencing static data members by class name
  28. cout<<a. Volume ( ) <<Endl; //Call volume function, calculate volume, output result
  29. }
The output of the above 3 output statements is the same (all 10). This verifies that the static data members of all objects are actually the same data members.

Note that in the above program, the height is defined as a common static data member, so you can refer directly outside the class. You can see that a public static data member can be referenced outside the class by object name, or a static data member can be referenced through the class name. Even if you do not define a class object, you can refer to a static data member by its class name. This means that a static data member does not belong to the object, but rather belongs to the class, but the object of the class can reference it. If a static data member is defined as private, it cannot be referenced directly outside the class, but must be referenced through a public member function.

5) with static data members, the data between the objects has a channel of communication, to achieve data sharing, so you can not use global variables. Global variables break the principle of encapsulation and do not conform to the requirements of object-oriented programs. However, note that public static data members differ from global variables in that the scope of a static data member is limited to the scope of the definition of the class (if the class is defined in a function, the scope of the static data member is within this function). Within this scope, static data members can be referenced by the class name and the domain operator "::", regardless of whether the class object exists.

C + + static data members and static member functions

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.