Static members of C + + classes

Source: Internet
Author: User

Static members of a class are divided into static data members and static member functions

  Static data members

If we want to design a war game, There are many classes in the Game. In the course of the game, every class will be produced at intervals, and the number of soldiers per class will decrease as a result of the war. For the plot more realistic, we introduced a concept of morale, when the morale is relatively high, the morale of this class will be very strong, morale is lower, the combat effectiveness of the class will be weaker. The morale of the class is affected by many factors, one of the most immediate factors is the number of soldiers involved, which is proportional to the number. We need a set of global variables, each of which will record the number of current classes, but using global variables introduces a lot of problems, the security of using global variables is not guaranteed, we can modify its value anywhere in the Program. Global variables can also cause namespace pollution, and when the program is large, there is a possibility of collisions between the various Modules. So if we don't use global variables, we can use static data Members.

static data member: begins with static . Static data members are common to individual objects and are not part of a specific object, and all objects can be referenced and read and Modified. If an object modifies the value of the static member, the value of the data member in each of the other objects is changed at the same time.

When you define a class, you allocate space for static data members without allocating space with the Object's Creation. It can be said that it belongs to this class, which can be referenced after the class is Defined.

  1. Defining Methods

  static int count;// defines an int type of data member, which is defined in the class, but initialization cannot be initialized outside the Class's inside class (you can initialize it in the implementation file of the class):

  int CTime::count=0;// Initialize do not add static

  2. Calling Methods

  1) class name :: static member

  2) object name . Static Members

A static data member within a class is simply a declaration of that member, and it needs to be defined outside the class.

when we set it to + + in the constructor and then output it, inside the destructor - The output gets the result: the number of times the constructor is called is the number of objects created, and the number of times the destructor is called is the number of times the object is Destroyed.

  Static member functions

Static member functions are defined when you add the static keyword before a function when declaring a member function .

As with static data members, static member functions are part of a class.

  1. declaring a static member function

  static int func (); The static keyword is not required when defining

  2. calling the static member function

  1) class name :: static member function

  2) object name . Static member functions

Static member functions are generally intended to handle static data Members.

  differs from the general member Function:

  1) The Non-static member function has this pointer, and the static member function does not have the this pointer.

  2) because it can be referenced when no class object is Defined. therefore, a static member function cannot access a non-static member of this class (without the this pointer, the member function and data member cannot be called by reference).

Original Link:http://www.maiziedu.com/wiki/cplus/static/

Static members of C + + classes

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.