Object-oriented deep learning after class (detailed explanation of static members of class C ++)
I just applied for a blog today and wrote the essay all afternoon. So I decided to come to the end of c ++ and I will take the exam next week, I don't know much about the static members of this category. I saw a very detailed blog on the Internet and took a look back.
In C ++, static members belong to the entire class rather than an object, and static member variables are only stored for all objects to share. So it can be shared among all objects. Using static member variables to share data among multiple objects does not undermine the hidden principle, ensuring security and saving memory.
The definition or declaration of static members requires a key static. Static members can be used by double colons, that is, <class name >:< static member name>.
In C ++, static member variables and static member functions of the class are prone to errors. This article uses several examples to summarize the rules for using static member variables and member functions, let's give an example to help you better understand the situation. I hope to read this article to give readers a deeper understanding of static member variables and member functions of the class.
In the first example, a class name is used to call static and non-static member functions.
1 class Point 2 { 3 public: 4 void init() 5 { 6 } 7 static void output() 8 { 9 } 10 }; 11 void main() 12 { 13 Point::init(); 14 Point::output(); 15 }
Compilation error: error C2352: 'point: init ': illegal call of non-static member function
Conclusion 1: You cannot call non-static member functions of a class by using the class name.
The second example calls static and non-static member functions through class objects.
Change main () of the above example:
1 void main() 2 { 3 Point pt; 4 pt.init(); 5 pt.output(); 6 }
Compiled.
Conclusion 2: The class objects can use static and non-static member functions.
The third example is to use non-static members of the class in the static member function of the class.
1 #include <stdio.h> 2 class Point 3 { 4 public: 5 void init() 6 { 7 } 8 static void output() 9 { 10 printf("%d\n", m_x); 11 } 12 private: 13 int m_x; 14 }; 15 void main() 16 { 17 Point pt; 18 pt.output(); 19 }
Compilation error: error C2597: illegal reference to data member 'point: m_x 'in a static member function
Because static member functions belong to the entire class, space has been allocated before class Object Instantiation, and non-static members of the class must have memory space after Class Object Instantiation, therefore, an error occurs in this call, just like using a variable without being declared.
Conclusion 3: Non-static members cannot be referenced in static member functions.
In the fourth example, the static member of the class is used in non-static member functions of the class.
1 class Point 2 { 3 public: 4 void init() 5 { 6 output(); 7 } 8 static void output() 9 { 10 } 11 }; 12 void main() 13 { 14 Point pt; 15 pt.output(); 16 }
Compiled.
Conclusion 4: Non-static member functions of a class can call static member functions, but not vice versa.
The fifth example uses static member variables of the class.
1 #include <stdio.h> 2 class Point 3 { 4 public: 5 Point() 6 { 7 m_nPointCount++; 8 } 9 ~Point() 10 { 11 m_nPointCount--; 12 } 13 static void output() 14 { 15 printf("%d\n", m_nPointCount); 16 } 17 private: 18 static int m_nPointCount; 19 }; 20 void main() 21 { 22 Point pt; 23 pt.output(); 24 }
Press Ctrl + F7 to compile without errors. Press F7 to generate the EXE program and report a link error.
Error LNK2001: unresolved external symbol "private: static int Point: m_nPointCount "(? M_nPointCount @ Point @ 0HA)
This is because the static member variables of the class must be initialized before use.
Add int Point: m_nPointCount = 0 before the main () function;
There is no error in compiling the link again, and the running program will output 1.
Conclusion 5: Static member variables of the class must be initialized before use.
Based on the above five examples, we will summarize the static member variables and member functions of the class:
I. Non-static members cannot be called in static member functions.
II. Static members can be called in non-static member functions. Because static members belong to the class itself, they already exist before class objects are generated, so static members can be called in non-static member functions.
3. Before using static member variables, you must first initialize them (for example, int MyClass: m_nNumber = 0;). Otherwise, an error occurs during linker.
Let's give an example of using static member variables and functions of the class for better understanding. This example creates a student class, and each student class object is grouped into a two-way linked list, use a static member variable to record the header of the two-way linked list. A static member function outputs the two-way linked list.
1 # include <stdio. h> 2 # include <string. h> 3 const int MAX_NAME_SIZE = 30; 4 5 class Student 6 {7 public: 8 Student (char * pszName); 9 ~ Student (); 10 public: 11 static void PrintfAllStudents (); 12 private: 13 char m_name [MAX_NAME_SIZE]; 14 Student * next; 15 Student * prev; 16 static Student * m_head; 17}; 18 19 Student: Student (char * pszName) 20 {21 strcpy (this-> m_name, pszName); 22 23 // create a two-way linked list, insert new data from the head of the linked list. 24 this-> next = m_head; 25 this-> prev = NULL; 26 if (m_head! = NULL) 27 m_head-> prev = this; 28 m_head = this; 29} 30 31 Student ::~ Student () // The Destructor process is the node's detachment process 32 {33 if (this = m_head) // The node is the header node. 34 {35 m_head = this-> next; 36} 37 else 38 {39 this-> prev-> next = this-> next; 40 this-> next-> prev = this-> prev; 41} 42} 43 44 void Student: PrintfAllStudents () 45 {46 for (Student * p = m_head; p! = NULL; p = p-> next) 47 printf ("% s \ n", p-> m_name); 48} 49 50 Student * Student: m_head = NULL; 51 52 void main () 53 {54 Student studentA ("AAA"); 55 Student studentB ("BBB"); 56 Student studentC ("CCC "); 57 Student studentD ("DDD"); 58 Student student ("MoreWindows"); 59 Student: PrintfAllStudents (); 60}
The program will output:
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/6721430
After reading it carefully, I felt that the author wrote very well and it inspired me a lot. Haha
In addition, I feel that there is still a small flaw, that is, the Destructor can be more rigorous.
1 Student ::~ Student () // The Destructor process is the node disconnection process 2 {3 if (this = m_head) // The node is the header node. 4 {5 Student * ptmp = m_head; 6 m_head = this-> next; 7 cout <"in destructor head" <endl; 8} 9 else if (this-> next = NULL) 10 {this-> pre-> next = NULL ;} 11 else {12 this-> prev-> next = this-> next; 13 this-> next-> prev = this-> prev; 14} 15}
I added
Else if (this-> next = NULL)
{This-> pre-> next = NULL ;}
Otherwise, this-> next-> prev may be faulty, because this-> next is NULL. How can I find the prev domain?
Test: new * p1 =...
New * p2 =...
New * p3 =...
// Separate delete p3 is fine
// Separately delete p2 is fine
Separate but delete p1 is wrong // This is the last one, because it is a forward insert
The modified destructor has no such problems. You can call the Destructor normally.
2016-06-21
20:13:22