In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "instance analysis of static member variables of C++ class" with detailed contents, clear steps and proper handling of details. I hope that this article "instance analysis of static member variables of C++ class" can help you solve your doubts.
I. Review of member variables
Public member variables can be accessed through object names
The member variables of each object are exclusive.
Member variables cannot be shared between objects
Second, new demand
Count the number of objects in a class during the run of the program
Ensure the security of the program (global variables cannot be used)
You can get the number of current objects at any time
Let's look at an example:
# include int gCount = 0; class Test {private: int mCount;public: Test (): mCount (0) {gCount++;} ~ Test () {--gCount;} int getCount () {return gCount;}}; Test gTest;int main () {Test T1; Test T2; printf ("count =% d\ n", gTest.getCount ()) Printf ("count =% d\ n", t1.getCount ()); printf ("count =% d\ n", t2.getCount ()); return 0;}
The output is as follows:
Although this code can get the number of currently defined objects, but the use of global variables can not guarantee the security of the program, so what is the good way? Look at the following.
Static member variables
Static member variables can be defined in C++
Static member variables belong to the entire class
The lifetime of static member variables does not depend on any object
You can access public static member variables directly through the class name
All objects share the static member variables of the class
You can access public static member variables through object names
Characteristics of static member variables
Directly modified by the static keyword when defining
Static member variables need to allocate space separately outside the class
Static member variables are located in the global data area within the program
Grammar rules:
Type ClassName:VarName = value
Let's look at the use of static member variables:
# include class Test {private: static int cCount;public: Test () {cCount++;} ~ Test () {--cCount;} int getCount () {return cCount;}}; int Test::cCount = 0TestTest gTest;int main () {Test T1; Test T2; printf ("count =% d\ n", gTest.getCount ()) Printf ("count =% d\ n", t1.getCount ()); printf ("count =% d\ n", t2.getCount ()); Test* pt = new Test (); printf ("count =% d\ n", pt- > getCount ()); delete pt; printf ("count =% d\ n", gTest.getCount ()); return 0;}
The output is as follows:
Test* pt = new Test (); A test object is dynamically generated in the heap space, so a call to the constructor is triggered, so count becomes 4 and delete pt; becomes 3.
After reading this, the article "instance Analysis of static member variables of C++ Class" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.