In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Class A {public: int foo () {return val;} static int staFun () {return staVal;} static int staVal; private: int val; char bit1;}; class B: public A {public: char foo () {return bit2;} private: char bit2;}
Static data members:
It is proposed by the compiler outside of class and is treated as a global variable (but only visible within the life range of class)
Each static data member has only one entity, which is stored in the data segment of the program
Via'.' Operator to access a static data member is just a grammatical bargain. The static data member is not actually in the class object, so it does not need to be accessed through the class object.
Although you can access a static member without relying on class object, its access function has to be tied to a class object. (if the access control of a static member is protected or private, it must be accessed through an access function)
[note:] the static data members of the class must be defined globally before they can be used.
Regardless of its access control, static data members must be defined before the main function.
For example: int A::staVal = 0
Otherwise, the compiler reports an error: an undefined reference.
Because the compiler converts the use of static data members to direct use, static members are declared in class and cannot be seen externally. Therefore, it needs to be defined externally so that the subsequent code is visible.
Handling of member functions:
One of the design criteria of C++: non-static member functions have at least the same storage efficiency as general external functions.
The C++ compiler internalizes member functions into normal functions:
① rewrites the function prototype and inserts an additional parameter this pointer. Used to provide an access pipeline so that the class object can call the function.
Int A::foo (A * const this)
If the member function is const, it becomes:
Int A::foo (const A * const this)
The access operation of ② to the non-static data members of the class object in the function body is accessed through the this pointer.
Int A::foo (A * const this)
{return this- > val;}
③ rewrites the member function as an external function and processes the function name to make it unique in the program.
After the above conversion operation is completed, each invocation operation is converted.
An objA; A * ptr = & objA; ptr- > foo (); objA.foo (); / / converted to: foo_intA (ptr); foo_intA (& objA)
Static member functions:
The main feature of a static member function is that it does not have a this pointer.
Therefore, it:
① it cannot directly access non-static members of its class
② cannot be directly declared as const, virtual.
③ it does not need to be called via class object-although most of the time it is called this way.
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.