Object-oriented static keywords (static keywords in the class)

Source: Internet
Author: User

From: http://blog.csdn.net/xiayefanxing/article/details/7382192

1. static data member

Add the keyword static before the declaration of the data member in the class. The data member is the static data member in the class.

Here is an example of a static data member.

1 // Example 5 2 # include "stdafx. H "3 # include <iostream> 4 using namespace STD; 5 class myclass 6 {7 public: 8 myclass (int A, int B, int C ); 9 10 void getsum (); 11 private: 12 int a, B, C; 13 static int sum; // declare static data member 14}; 15 int myclass :: sum = 0; // defines and initializes static data member 16 myclass: myclass (int A, int B, int c) 17 {18 this-> A =; 19 this-> B = B; 20 this-> C = C; 21 Sum + = A + B + C; 22} 23 void myclass: getsum () 24 {25 cout <"sum =" <sum <Endl; 26} 27 void main () 28 {29 myclass M (1, 2, 3); 30 m. getsum (); 31 myclass N (4, 5, 6); 32 N. getsum (); 33 m. getsum (); 34 getchar (); 35}

It can be seen that static data members have the following features:

For non-static data members, each class object has its own copy. Static data members are treated as class members. No matter how many objects are defined for this class, static data members also have only one copy in the program, which is shared by all objects of this type. That is to say, static data members are shared by all objects in the class. For multiple objects in this class, static data members only allocate memory once for all objects to share. Therefore, the values of static data members are the same for each object, and their values can be updated;

Static data members are stored in the global data zone. Space must be allocated when defining static data members, so they cannot be defined in the class declaration. In Example 5, the statement int myclass: Sum = 0 is a member that defines static data;

Static data members follow the public, protected, and private access rules like common data members;

Because static data Members allocate memory in the global data zone and all objects in this class are shared, they do not belong to a specific class object and are visible in the scope when no class object is generated, that is, when no class instance is generated, we can operate on it;

 

  Static data member Initialization is different from general data member initialization. 

  The static data member initialization format is: 

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

 

Static data members of a class can be accessed in two forms:

<Class Object Name>. <static data member name> or <class type name >:< static data 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 above format;

Static data members are mainly used when each object has the same attribute. For example, for a deposit type, the interest of each instance is the same. Therefore, the interest should be set as the static data member of the deposit class.

There are two advantages: first, no matter how many deposit-type objects are defined, interest data members share the memory allocated to the global data area, thus saving storage space.

Second, once the interest needs to be changed, the interest of all deposit objects will be changed once;

 

Compared with global variables, static data members have two advantages:

  The static data member does not enter the global namespace of the program, so there is no possibility of conflict with other global names in the program; 

Information can be hidden. Static data members can be private members, but global variables cannot;

 

2. static member functions

Like static data members, we can also create a static member function that serves all the classes rather than specific objects of a class. Static member functions, like static data members, are internal implementation of the class and are part of the class definition. A common member function generally implies a this pointer, which points to the object itself of the class, because a common member function always belongs to a specific object of a class. Generally, this is the default value. For example, the function FN () is actually this-> FN (). However, compared with common functions, static member functions do not have the this pointer because they are not associated with any objects. In this sense, it cannot access non-static data members of class objects or non-static member functions. It can only call other static member functions. The following is an example of a static member function.

1 // Example 6 2 # include "stdafx. H "3 # include <iostream> 4 using namespace STD; 5 class myclass 6 {7 public: 8 myclass (int A, int B, int C ); 9 static void getsum (); // declare static member function 10 private: 11 int A, B, C; 12 static int sum; // declare static data member 13 }; 14 int myclass: Sum = 0; // defines and initializes static data member 15 myclass: myclass (int A, int B, int C) 16 {17 this-> A = A; 18 this-> B = B; 19 this-> C = C; 20 sum + = A + B + C; // non-static member functions can access static data member 21} 22 void myclass : Getsum () // The Implementation of the static member function. The implementation cannot be static! 23 {24 // cout <A <Endl; // error code. A is a non-static data member 25 cout <"sum =" <sum <Endl; 26} 27 void main () 28 {29 myclass M (1, 2, 3); 30 m. getsum (); 31 myclass N (4, 5, 6); 32 N. getsum (); 33 myclass: getsum (); 34 getchar (); 35}

Static member functions can be summarized as follows:

 

The keyword static cannot be specified for function definitions that appear in external classes;

Static members can access each other, including static member functions accessing static data members and accessing static member functions;

Non-static member functions can access static member functions and static data members at will;

 

Static member functions cannot access non-static member functions and non-static data members !!!

Without the additional overhead of this pointer, static member functions increase slightly compared with global functions of the class;

 

Call static member functions. You can use the member access operators (.) and (->) to call static member functions for a class object or a pointer to a class object.

Object-oriented static keywords (static keywords in 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.