Referenced from: 78122485
- Constructor's default constructor (the calling constructor does not pass parameters)
Both instantiation methods are default constructors
- Initialization list of constructors
The Scarlet Letter section is the initialization list: A quotation mark, multiple parameters separated by commas, and an assignment () to initialize the list attribute:
- Its first in constructor execution
- It can only be used for constructors
- It can initialize multiple data members at the same time
- "Note" The function of the initialization list: for static constants in a class that cannot be initialized with constructors, you must use the initialization list
Features of the copy constructor:
-
- When the user does not have a constructor defined, the compiler automatically generates a default copy constructor (as well as a constructor)
- The copy constructor is automatically called when instantiating an object with direct initialization or replication initialization
- Because of the nature of the copy constructor, the copy constructor cannot be overloaded!!
- Summarize constructor Summary:
- "Summary" When all parameters have default values, they are converted to default constructors
"Note" The initialization list is available for all constructors.
- Destructors (functions for freeing system resources)
Define the format:
Destructor Features:
- If you do not have a custom destructor, the system automatically generates
- Destructors are called automatically when an object is destroyed (corresponding to which the constructor is automatically called when the object is instantiated)
- Destructors do not have return values (the same is true for constructors), no parameters, and cannot be overloaded
- Do not allow any parameters to be added within ()
- The life course of "important knowledge point" object
- Classification of "Summary" member functions
Parameter decoration method of member function
How objects are instantiated
Define a function as long as it is not a function declaration, remember to write {}, even if {} does not write any statements.
Summary: The order in which constructors and destructors are called
1. Execute the constructor of the grouped object first
(1) When a class has a member variable that is an object of another class, the constructor for the member variable is called first, the invocation order is the same as the declaration order, and then the constructor of the class is called.
(2) The order of the destructors is called in the opposite order of the corresponding constructor calls.
2, the construction order of the grouped objects, the predefined order has a relationship, and the order of the initialization list is not related
3. Special member variables must be initialized with a parameter list. const, &, Object .
4. A member variable of typestatic must be initialized outside of the class without the static keyword.
List of constructor parameters for C + +