In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor would like to share with you the relevant knowledge of what C++ 's static members of static are. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
I. static members
That is, members of the definition preceded by the static keyword.
# includeusing namespace std;class A {public: a (int a, int b): Macroa (a), mubb (b) {num + = massia + madopb;} ~ A () {} void Fun () / / different member functions static void PrintNum () {/ / static member functions / / in static member functions, non-static member variables cannot be accessed / / and non-static member functions std::cout PrintNum () cannot be called; / / reference. Member name An a tera & ref = a terref. PrintNum (); V. Summary
Static member variables are essentially global variables, even if there is no object, the static member variables of the class still exist; similarly, static member functions are essentially global functions.
The purpose of setting static members is to write certain global variables and functions in classes that are closely related to each other, making them more "integrated" and easier to maintain and understand.
You cannot access non-static member variables or call non-static member functions in static member functions.
Static members must initialize static member variables in the file that defines the class, or there will be compilation errors.
With regard to the last point above, it is also often tested in interviews:
Static members must initialize static member variables in the file that defines the class
In C++, static members of a class (static member) must be declared within the class and initialized outside the class.
Static members cannot be initialized within a class-- because static members belong to the entire class, not to an object, and if initialized within a class, it is contradictory that every object contains that static member.
What can be initialized in a class
There is only one member that can be initialized in a class, and that is a static constant member.
Error 1 (static members cannot be initialized within a class):
Class A {private: static int count = 0; / / static members cannot be initialized within a class}
Error 2 (constant members cannot be initialized within a class):
Class A {private: const int count = 0; / / constant members cannot be initialized within a class.
Correct (static constant members can be initialized within the class):
Class A {private: static const int count = 0; / / static constant members can be initialized within the class}; VIII. Initialization order of C++ static type member variables
The initialization order of variables should be:
1 static or global variables of the base class
2 static or global variables of a derived class
3 member variables of the base class
4 member variables of derived classes
When a member variable is initialized with an initialization list, it has nothing to do with the order in which the member list is initialized in the constructor, but only with the order in which the member variable is defined. Because the initialization order of member variables is related to the order of variables in memory, and the order of arrangement in memory is determined according to the definition order of variables as early as the compilation time. This is described in detail in EffectiveC++.
The order in which static variables are initialized is that the static variable of the base class is initialized first, followed by its derived class. Until all static variables are initialized. It is important to note that global and static variables are initialized out of order (in fact, both static and global variables are placed in the public memory area). Static variables can be understood as global variables with "scope". After all initialization work, the main function is called, and if the constructor of a class is executed, the member variables of the base class are initialized first.
That's all of the article "what are the static static members of C++?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.
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.