Notes for handling C ++ static members

Source: Internet
Author: User

C ++ static members are proposed to solve the problem of data sharing. There are many ways to achieve sharing, such as setting global variables or objects. However, global variables or objects have limitations. In this chapter, we mainly introduce static members of the class to share data.

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. C ++ 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:

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 static data members:

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, that is, 54.

It is worth noting that a trap needs to be avoided here, that is, once people determine that "C ++ is not good", this reason will "grow your feet", that is, even if we get rid of the complexity of C ++, they may insist that they still don't need C ++ static members and find a bunch of reasons for them. I assume you are not such a person.

However, perhaps most likely, he would say, "The problem is that the C ++ we are using today is not so concise.) your assumptions are not true ." Yes, my assumptions are not true. However, although we cannot eliminate complexity, we can avoid complexity and short-term growth. This is also the main point of this article. I will discuss it later.

Of course, you may say it now. I still don't need C ++, because I can use D; or if you don't need C ++ for a project, you may say that I use Python. First, if your project can be implemented using Java, Python, or Ruby, then using C ++ is a tough option. What is the value of using a language that represents your project in terms of efficiency is not demanding, so you cannot find too many benefits in terms of efficiency and complexity? Second, if your project efficiency is very important, you may say you can use D.

However, the reality is that D is rarely used in the industry, especially in China. C ++ has a lot of existing code. Companies that have already used C ++ for their products cannot rewrite the code in other languages for a long time, as Joel said, he decided to rewrite an extraordinary code base = to commit suicide.

  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.