Constructor is a special method in the development process. It is mainly used to initialize an object when an object is created, that is, to assign an initial value to an object member variable, when a C ++ constructor is used in C ++, whenever a class or structure is created, its constructor is called.
However, after the program passes the compilation check, it does not mean that the error does not exist. In the "error" family, the "syntax error" status can only be regarded as a younger brother. High-level errors are often hidden in depth, just like a sly criminal. It is not easy to catch him.
Based on experience, many program errors that are hard to detect are caused by the variable being correctly initialized or cleared, and initialization and cleanup are easily forgotten. Stroustrup fully considers this problem when designing the C ++ language and solves it well: it puts the initialization of the object in the C ++ constructor, put the cleanup work in the destructor. When an object is created, the constructor is automatically executed. When an object dies, the Destructor is automatically executed. You don't have to worry about object initialization and cleanup.
The name of the constructor and the Destructor cannot start at will, and must be recognized by the compiler before it can be automatically executed. The naming method of Stroustrup is simple and reasonable: the constructor and destructor have the same name as the class. because the purpose of the constructor is opposite to that of the constructor, the prefix '~ 'To show the difference.
Except for the name, the constructor and the Destructor have no return value type, which is different from the void function. The mission of C ++ constructor and destructor is very clear, just like birth and death. If they have return value types, the compiler will be overwhelmed. In order to prevent out-of-the-box branches, it is simply specified that there is no return value type. The above reference [Eekel, p55-p56])
The C ++ constructor has a special initialization method called "initialization expression Table ). The initialization table is located after the function parameter table, but before the function body. This indicates that the initialization work in the table occurs before any code in the function body is executed.
Rules for using constructors to initialize tables:
If the class has an inheritance relationship, the derived class must call the base class constructor in its initialization table.
For example:
- Class F
-
- {
-
- Public:
-
- F (int x, int y); // Constructor
-
- Private:
-
- Int m_x, m_y;
-
- Int m_ I, m_j;
-
- }
Class data members can be initialized using the initialization table or function body assignment method, the efficiency of these two methods is not exactly the same, non-Internal data type member objects should be initialized in the first way to achieve higher efficiency.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code