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 points for attention of the static members of C++?

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

Share

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

This article will explain in detail what you should pay attention to in learning C++ static members. The content of the article is of high quality, so the editor shares it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The following focuses on C++ static members. Static data members of a class should be initialized in the code and must be carried out outside the class. C++ static members are quite different from ordinary data members, which are reflected in the following points:

1. Static data member

The way to define a C++ static member in a class is to precede the member with the keyword static.

The statement format for defining static data members is as follows:

Class class name {... Static type specifier member name;... }

C++ static members are members that are shared by all objects of the class. The space occupied by static data members is not allocated with the generation of the object, nor is it recycled as the object disappears. The operation on static data members is different from that of general data members in the class, and static data members defined as private cannot be accessed by the outside world. Static data members can be accessed by functions with arbitrary access permissions.

Because the static data member is shared by all objects of the class and is not subordinate to any specific object, the static data member of the class must be initialized, but its initialization cannot be done in the constructor of the class. Its initialization statement should be written in the global area of the program, and must indicate its data type and the class name to which it belongs. The initialization format is as follows:

Type class name:: variable name = value The static data members described in the public section of the class can be accessed directly outside the class without using member functions, but the class name must be used to indicate the class to which they belong. The access format is: for the static data members described in the non-public part of the class, they can only be accessed by the member functions of the class, and their access methods are exactly the same as those of ordinary data members in the class, but cannot be accessed outside the class.

two。 Static member function

The definition of a static member function is the same as that of a general member function, except that it is preceded by the static keyword, which is defined in the following format:

Class class name {... Static type function name (formal parameter) {function body}... }

Description:

(1) C++ static member functions of a class can only access static data members of the class, but not ordinary function members (non-static data members) in the class, because ordinary data members are meaningful only when the object of the class exists.

(2) the C++ static member function is associated with the class, not the object of the class, so the public static member function in the class that is called outside the class must add "class name::" to its left, instead of calling the public static member function through the object name. Private static member functions in the class cannot be called outside the class.

What are the notes on learning C++ static members? so much for sharing here. I hope the above content can be helpful to you and you can learn more knowledge. If you think the article is good, you can share it for more people to see.

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