Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the considerations when dealing with the static members of C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares with you is about the notes when dealing with the static members of C++. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it with the editor.

The static members of C++ 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 are limited. In this chapter, we mainly talk about the static members of the class to achieve data sharing.

Static data member

In a class, static members can achieve data sharing among multiple objects, and the use of static data members does not break the hidden principle, that is, security is guaranteed. Therefore, static members are members that are shared in all objects of a class, not members of an object.

Using static data members saves memory because it is common to all objects, so for multiple objects, static data members are stored in only one place that all objects share. The value of a static data member is the same for each object, but its value is updatable. As long as the values of static data members are updated once to ensure that all objects access the same value after the update, this can improve time efficiency.

The usage and considerations of static data members are as follows:

1. Static data members are defined or described with 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:

:: =

This shows that:

(1) initialization is carried out outside the class without static in front of it, so as not to be confused with general static variables or objects.

(2) the access control character private,public of the member is not added during initialization.

(3) scope operators are used to identify the class to which it belongs during initialization, so static data members are members of the class, not objects.

3. The static data member is statically stored, it is static lifetime, and it must be initialized.

4. When referencing static data members, the format is as follows:

If access to static data members is allowed (that is, members of public), static data members can be referenced in the above format in the program. Here is an example to illustrate the application of static data members:

From the output, you can see that the value of Sum is equal to both M object and N object. This is because when the M object is initialized, the value of the three int data members of the M object is summed and assigned to Sum, so Sum saves the value. When initializing the N object, the values of the three int data members of the N object are summed and added to the existing values of the Sum, so the Sum will save the other values. Therefore, the value referenced by either object M or object N is the same, that is, 54.

It is worth noting that there is a trap to be avoided here, that is, once people decide that "C++ is bad", then this reason will "grow their own feet", that is, even if we remove the complexity of C++, they may also insist that they do not use C++ static members and find a bunch of reasons for it. I assume you're not like that.

However, perhaps the most likely thing is that he will say: "the problem is that the C++ we use today is not so (concise), and your hypothesis is not valid." Yes, my hypothesis is not valid. But although we cannot eliminate complexity, we can actually easily avoid complexity and avoid weaknesses and strengths. This is also the main point of this article, let me elaborate later.

Of course, you might still say. I still don't need clocking because I can use D, or if the project you're working on doesn't need clocking, you might say, "I use Python." First of all, if your project can be done in Java/Python or even Ruby, then using C++ is asking for trouble. Because it is not very efficient to represent your project in those languages, what is the value of using a language that is less efficient but more complex? Second, if your project efficiency is important, you might say you can use D.

However, the reality is that D is rarely used in industry, especially in China. C++, on the other hand, has a lot of existing code, and companies that have used C++ to make their products will be almost impossible to rewrite code in other languages for a long time, as Joel said, decided to rewrite a non-trivial code base.

The above is what to pay attention to when dealing with the static members of C++. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report