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

How to share C++ data in Static

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

Share

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

This article introduces how to share C++ data in Static. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

C++ language is an extension of C language, that is, a C language program, can also be regarded as a C++ program, for C++ classes, static members can achieve data sharing between multiple objects.

Static data member

In C++ 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, to ensure security. 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. 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:

# include class Myclass {public: Myclass (int a, int b, int c); void GetNumber (); void GetSum (); private: int A, B, C; static int Sum;}; int Myclass::Sum = 0; Myclass::Myclass (int a, int b, int c) {A = a; B = b; C = c; Sum + = Aging Barrier C;} void Myclass::GetNumber () {cout

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