In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to implement static member functions and static member variables in C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to implement static member functions and static member variables in C++.
Review the static keywords in C language
(1) add the local variable in front of it to make it a static local variable, the scope is still inside the function, but the life cycle is prolonged.
(2) add in front of the global variable to define the scope of the variable as a file scope, that is, even if other files use the extern extension scope. This is very useful in multiplayer projects in C language, avoiding the duplicate names of variables. However, this function has been replaced by namespaces in C++, but in order to maintain compatibility with the C language, static still has this function.
(3) add in front of the function definition or declaration to limit the scope of the function to the file scope, but also to avoid multiple files with duplicate functions.
When the static keyword appears in the class
Static member variables and static member functions appear when static appears in the definition of a class. Static members belong to a class, not to an object. Even without any instance, the static member variable of the class already exists and may be accessed through the "class name:: member name". Static member functions of a class can also be called in the same way, calling member methods before the class generates an instance. A typical application is to implement the singleton pattern.
(1) static member variables
Static member variables are essentially global variables, but writing global variables that are closely related to some classes into the class makes it formally a whole and easier to understand and maintain. So try to use static member variables to reduce the use of global variables. Each object has its own copy of ordinary member variables, but there is only one static member variable, which is shared by all objects of this class. If you use the sizeof operator to calculate the size of an object, the result does not include static member variables.
Static members are also subject to restrictions such as private,public.
A typical application of static member variables is to count the number of instances generated. The general idea is to set a static member variable named num and initialize it to 0, in the constructor + + num, in the destructor-- num. So the value of num is the number of current instances. In fact, this also brings a hidden bug. Look at the following code:
Class CNum {public: static int num; ~ CNum () {--num;} CNum () {+ + num;}}; int CNum::num = 0 fun (CNum n) {} int main () {CNum n; fun (n); fun (n); 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.