Constructor (including default constructor), use and characteristics of destructor


1. Constructor

The constructor is a special member function with the same name as the class name . It is automatically called by the compiler when creating a class type object to ensure that each data member has an appropriate initial value, and only during the entire life cycle of the object Called once. It should be noted that although the name of the constructor is called construction, the main task of the constructor is not to open space to create objects, but to initialize objects .

characteristic:

  1. The function name is the same as the class name.
  2. No return value.
  3. The compiler automatically calls the corresponding constructor when the object is instantiated.
  4. Constructors can be overloaded.

Let's look at the explicit constructor first
. When we define an object, the compiler has already completed the initialization.
If there are parameters and they are not all default types, we need to pass parameters when creating the object.
insert image description here

Second, the default constructor (also a constructor)

If no constructor is explicitly defined in the class , the C++ compiler will automatically generate a default constructor with no parameters. Once the user explicitly defines the compiler, it will no longer generate it.

Types of default constructors:

Note: No-argument constructors, full default constructors, and constructors that we did not write to be generated by the compiler by default can all be considered default constructors.

1. No parameter type

insert image description here

2. Full default type

insert image description here

3. Automatically generated by the compiler

Because it’s not easy to show here because we produce it ourselves, when we don’t write any constructor, the compiler will automatically generate a constructor.
insert image description here

4. Summary

What are built-in vs custom types?
1. Built-in types/basic types: basic types defined by the language itself: int double char float, etc.
2. Custom types: variables defined with struct/class/unio, etc.

We generally think simply that the default constructor does not need to pass parameters

Both the parameterless constructor and the default constructor are called default constructors, and there can only be one default constructor.insert image description here

Here we should pay attention to the constructor generated by the compiler
1. The built-in type is not processed.
2. The custom type will call his default constructor

Under what circumstances is the default constructor used?
1. In general, if there are built-in type members, you need to write the constructor yourself, and you cannot let the compiler generate it.
2. All are custom type members, you can consider letting the compiler generate it by itself.

3. Destructor

Destructor: Contrary to the function of the constructor, the destructor does not complete the destruction of the object itself, and the local object destruction is done by the compiler. When the object is destroyed, it will automatically call the destructor to complete the cleanup of resources in the object.

A destructor is a special member function whose characteristics are as follows:

  1. The destructor name is prefixed with the character ~ before the class name.
  2. No parameters and no return type.
  3. A class can have only one destructor. If not explicitly defined, the system will automatically generate a default destructor. Note: Destructors cannot be overloaded
  4. When the life cycle of the object ends, the C++ compilation system automatically calls the destructor.

insert image description here
Let's take a look at the running process:
insert image description here
insert image description here
the same as the constructor, if the user does not write it, the default destructor will also be generated

Default destructor function:
Built-in type members are not processed.
Custom type members will call its default destructor (only the default)

insert image description here

Guess you like

Origin blog.csdn.net/m0_74774759/article/details/130707825