12.6 static class member

Source: Internet
Author: User

At any point of the program, you need to count the number of created objects of a specific class type, or a pointer to the error handling process of the class, that is, the processing method for errors of all objects of the class ), or when an object in the class needs to access a common variable, such as the Account type in the bank, each account has information such as username, balance, and interest rate, and the interest rate is the same for the users used ). In the preceding circumstances, a global variable can be defined. However, a Global Object destroys encapsulation. Not only can this class be used to access this global object, but also can be accessed by user code. Therefore, a class can define static members rather than defining a universally accessible global object.

Static class members: Class Members with the keyword "static" before declaration. Static class members are not part of any object, but are shared by all objects of the class. Static class members include static data members and static function members.

The static member function does not have this parameter. It can directly access static members of the class, including static data members and other static member functions ).

Advantages of using static members:

1) The name of the static member is in the scope of the class, which can avoid conflicts with the names of other class members and global objects.

2) encapsulation is possible. static members can be private members, but global objects cannot.

3) by reading the program, you can easily see that static members are associated with a specific class, which clearly shows the programmer's intent.

Different from common members: common members are always associated with objects and are components of an object. static members are associated with classes and shared by all objects of this class, is not part of any object.

Use static members of a class

You can call static members directly from the class through the scope operator, or indirectly through objects, references, or pointers to objects of this type. For example, ac1.rate () // rate () must be public

Ac2-> rate () // rate () must be public

Account: rate ()

 

This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1289767

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.