I encountered a problem today. After debugging for a long time, I found that the misuse of class pointers and the fuzzy understanding of class pointers and class objects are the more basic things I have, the easier it is to have problems. Remember the lessons!
Problem description:
In my project, there is a public class that defines some shared methods, for example, class share.
In Class A, I declare a share pointer, share * share, and execute it to a function in Class A, and then directly call the function in class share through share->, in this function, another function in Class B (Class B also declares a share pointer, share * share) is called, and this function also needs to be called through share->, in this case, the compilation is correct. If the execution fails, the breakpoint is at the share of the function in Class B. PS: segmentation fault (segment error) is prompted in Linux ), it is actually a memory access error.
Three mistakes are made here:
1) When declaring a class pointer, you must initialize the pointer. Otherwise, it will become a wild pointer. Directly calling a function will certainly lead to problems;
2) because the previous pointer has not been released since it was called and remains in the memory, the subsequent declaration of the pointer with the same name will inevitably lead to memory access errors;
3) the class pointer should not be declared to call methods in the class. The object should be declared. The class pointer is mostly used for implementation of polymorphism and the pointer time is used to access the object.
The following describes the differences between class objects and class pointers:
Class pointer: it is a memory address value that points to class objects stored in the memory (including values assigned by some member variables ).
Object: the class constructor allocates a piece of memory in the memory (including the values assigned by some member variables ).
Application Time:
1. Reference members: the object uses the "." operator; the pointer uses the "->" operator.
2. life cycle: if it is a member variable, it is the class destructor to release space; if it is a temporary variable in the function, the scope is the function body. the pointer needs to use Delete to release the allocated memory block in the corresponding place.
Note: to use new, you must delete ..
Class Object: uses the memory stack and is a local temporary variable.
Class pointer: uses a memory heap, which is a permanent variable unless you release it.
When a class is a base class with virtual functions and func is a virtual function, call FUNC:
Class Object: calls its own func;
Class pointer: calls the func class that is assigned to it space;
What are the differences between a class object and the class pointer (memory allocation using the new operator) in the application?
1. Classes and objects are two different things. objects are class instances;
2. objects are allocated in the stack, and objects generated using new are allocated in the heap;
3. to make full use of virtual functions, you must use pointers to access objects.
Pointers can achieve polymorphism, but objects cannot be used directly.
Execute the definition object in the stack space
Heap at New
Note the name type.
One is student.
One is student *
Student is a direct access to an object
Student * is used to indirectly access an object because a pointer is used as a media.
The type determines what you can do.
In fact, basically the same function is used to call the member variables and member functions of the class.
When you want to explicitly use this class, it is best to use objects. If you want to use dynamic binding in C ++, it is best to use pointers or references.
Pointers and references are more flexible to use and can easily implement polymorphism.
One of the essence of C ++ is polymorphism. Only pointers or references can achieve polymorphism. No object
Pointer:
First, implement polymorphism.
Second, in the function call, pass the pointer parameter. No matter how large your object or structure parameters are, you can use a pointer to transmit four bytes in the past. If an object is used, passing parameters will occupy too much resources.