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 use static data members in C++

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use static data members in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn how to use static data members in C++.

In the development environment of C++, static member functions and static data members are static members of the class, and they are not object members. Therefore, references to static members do not need to use object names, but refer to non-static members.

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. 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.

The following is an example to illustrate the application of C++ 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