Recently, in order to find a job, I was despised during the interview. When I came back, I reviewed my basic knowledge. Here we will make a summary of some knowledge points as materials for future review and provide them to some cainiao like me as a reference.
Static knowledge points are from the Internet and forums. This article is messy and complex. If there are errors or deficiencies, I hope you will not be careful to see the heroes in this article and instruct them to correct them.
I. static variable Storage
Compared with the stack, static variables are allocated in the static data zone. This storage feature has the following applications.
1: global variables
Static modified variables or functions are global variables, but they are different from extern declarations.
First, static and extern cannot be used together.
Second, the static modified global variable declaration and definition must be performed simultaneously.
The global variables modified in static mode are limited to the compiling units.
Static files must be defined in the. cpp file. Only declarations are made for. H files to avoid unnecessary pollution to other modules.
2: One copy (this is a question that many interviewers will mention)
All static modified class member variables or functions belong to the internal implementation of the class and belong to the class definition part. Therefore, no matter the number of objects in the class, all objects share a static member in the memory. That is, only one copy of static modified variables and functions is available for all objects. (This feature can be solved, for example, how to make the class define only one object entity ). Usage: a definition must be provided during the Declaration, that is, the memory block is allocated.
A. Initialization
<Data type> <class name >:< static member >=< value>
For example, int myclass: mystatic = 0; // In the. cpp File
B. Use/Access
<Class name >:: <static member variable> or use it like a normal variable, for example:
Myclass: mystatic or myobjetc. mystatic
Ii. static member functions
(An interviewer asked me this question. I am stupid. Although I often use it, I really don't know the relevant knowledge points. He despised it. Since then, I have learned that I have many dishes, and the basic knowledge of C ++ is basically 0. When I came back, I started to read books, but now I am still a super cainiao. Keep learning ).
First, the static member functions are the same as the static member variables. They are all part of the class definition and stored in a shared static data zone.
Second, the static member function is a global variable, but its scope is limited to the current compilation unit.
Again, the static member function does not have the property that common member functions are associated with objects, so the static member function does not have the implicit this pointer. Therefore, static member functions cannot access non-static member variables or non-static member functions. That is why the overhead of accessing static member functions and variables is greater than that of common member variables and functions.
The access to static member functions can be summarized as follows:
1. Static members can access each other, including functions and variables.
2. Non-static members can access static members without restriction.
3. static member functions cannot access non-static member functions and non-static member data.
There are two ways to access static functions.
A: myobject. mystaticfuc ()
B: myclass: mystatcifuc ().