C ++'s tedious object-oriented mechanism has to make you frequently "ill", find errors, find bugs, and troubles in programming. It is the source of your illness. Although I am not a "good doctor ", however, it can be summed up as follows ". Note: It is inevitable that there will be memory omissions and insufficient knowledge. We need to add some omissions!
Prescription 1? You must always understand the two roles to play in Object-Oriented Programming. One is you, the designer of the class, the other is the user and the user of the class. Your design is for the user, of course, you may also have two roles, the designer and the user.
Prescription 2? When designing a class, place the declaration in the header file and the definition in the CPP file. Because the class is used by the user, put the declaration in the header file for convenient use, the definition is mainly used in CPP files for two purposes: first, to improve compilation efficiency, and second, to avoid redefinition after the user contains the file.
Prescription 3? After declaring a class, it must end with a semicolon, because after declaring a class, you can add an object name to declare the class's instantiated object, semicolon '; 'is not the end of the declaration, but the end of the declaration of this class object. If a semicolon is used, it means that no object is declared.
Prescription 4? Avoid instantiating this class in the constructor. Otherwise, recursive endless loops will occur.
Prescription 5? Avoid deleting this type of instances in the destructor. Otherwise, recursive endless loops will occur.
Prescription 6? Inline member functions must be defined in the header file, because inline functions are essentially expanded inline, similar to # define pre-compilation.
Prescription 7? Classes or functions in the HPP header file must be defined in the HPP header file, because the essence of HPP is to allow the compiler to compile the content in the HPP file only once, then, attach the target code to the CPP target file.
Prescription 8? The global variables in the HPP header file must be declared as static. Otherwise, recompilation will occur. The essence of static is to allow the compiler to define the global variables only once.
Prescription 9? The const data member must assign an initial value in the initialization list (C ++ 11 can also directly assign a value =) because it is a const variable.
Prescription 10? The const member function does not allow you to modify data members of a class. Because it is not allowed.
Prescription 11? When a pointer to a const object accesses a member function, you can only access the const member function, because the pointer to the const object does not allow you to modify the memory value it points, the const member functions do not modify the memory value of the Class Object Data. They exactly match each other.
Prescription 12? A static data member must be defined outside the class. It is not essentially a class that contains it.
Prescription 13? The static member function does not contain the this pointer because it does not belong to this class in nature.
Prescription 14? If a pointer data member in the class points to the allocated memory space, the general practice is to define the Destructor and release the memory space it refers to in the destructor. Because at the end of the class life cycle, if no destructor is defined, the generated destructor in the editor will be called, the compositing destructor does not intelligently release the memory pointed to by the data member pointer. Therefore, the memory directed to the data member must be released before the class ends. Another way is to define the member pointer as a smart pointer type.
Prescription 15? It is best not to overload two built-in types of objects for operator overloading. At least one of the overloaded types must be of the class type or enumeration type. Because it is unnecessary to overload two built-in types of objects.
Prescription 16? The value assignment operator is overloaded (operator =) and should not be set as a virtual function, because the pointer or reference function parameter in the value assignment operator may refer to the base class object or to the derived class object, so it is prone to confusion.
Prescription 17? In the inheritance level, the base class should define a virtual destructor because non-virtual destructor will not be inherited, the virtual destructor will be redefined when the derived class defines the Destructor (that is, the virtual table Pointer Points to the destructor of the derived class ). When the base class pointer is used to realize polymorphism, the base class Pointer Points to the newly opened (new) derived class object, because the base class destructor are virtual, therefore, when the base class pointer is used to release (delete) the object memory of the derived class, the destructor of the derived class can be smoothly executed.
Prescription 18? Template Class or function, declared in the header file, and the definition should also be in this header file (generally use the HPP header file ), because the template special mechanism is actually to use the template parameters to generate the target code compatible with the instance during compilation and attach it to the CPP file using it (special) to realize polymorphism, the target Code cannot be generated in advance to be compatible with the instance.
Prescription 19? Before declaring a friend, you must declare or define the object of the declared friend. Because you asked me to make friends with him, yes, but you must let me know him. Pai_^
Prescription 20? What you know is nothing more than pride and complacency.