A constructor is a special member function. Called automatically when the object is created, initializing the data members of the object.
(1) The object created in the stack will automatically call the destructor at the end of the lifetime.
(2) The object created on the heap, to be displayed by the programmer, calls delete to release the object while invoking the destructor.
(3) The construction of the global object precedes the main function. The global object also automatically calls the analysis constructor at the end of its lifetime to release the object.
Functions of constructors: initialization, type conversion (conversion constructors)
1. Conversion constructor
Definition: A transformation constructor is a constructor of a single parameter
Role: Converting other types to class types
The constructor of a class has only one parameter that is very dangerous, because the compiler can use this constructor to implicitly convert the type of the argument to the class type
The constructors with one parameter are:
Common constructors
Conversion constructors (initialization, type conversions)
2. Difference between assignment and initialization
The equals sign in the initialization statement is not an operator. The compiler has a special explanation for this representation
Assignment test& test::operator= (const test& Other);
3.explicit
Keywords that are only available to constructors of classes
The compiler does not use a constructor declared as explicit for implicit conversions, it can only display the created object in program code
C + + Note----constructor and destructor (ii)