Exploring C++ again - default member functions

Table of contents

1. Constructor

Second, the destructor

3. Assignment operator

4. Copy construction


If there are no members in a class, we call it an empty class. An empty class also has 6 default class member functions.

Default member function: The function that the user writes without displaying it, and the compiler will generate it by default is called the default member function.

6 default member functions:
Constructor: Complete object initialization Destructor: Complete object cleanup

Copy construction: Construct a new class from objects of the same type Assignment overloading: Assign one object to another

Take address overloading: rarely involved

1. Constructor

In C language, the simulation of a data structure must first complete the initialization, take the stack as an example:

The initialization needs to determine the top, the space to be opened up, and the initial capacity of the array. If this initialization is not completed, many problems will arise. In order to prevent the initialization of the class from being forgotten, a constructor is introduced.

When the class is created , it is called automatically for initialization.

1. Features:

1) The name is the same as the class name and there is no return value.

2) Instead of creating an object in open space, it is called automatically after instantiation.

3) can be overloaded

Take the date class as an example

.Constructor can be overloaded    overloaded to pay attention to ambiguity

.About overloaded calls

Date d1() cannot be written when calling a no-argument constructor ; otherwise, the compiler will consider this a function declaration .

.give default values ​​in function overloading

If the function with parameters is given all defaults , then it and the constructor without parameters are both default constructors, and the compiler can initialize without the user's explicit call.

Only one default constructor can appear , and the full default cannot appear at the same time as the no-argument constructor.

 There are no-argument constructors and all-default constructors in the class. The compiler can pass the compilation, but there will be ambiguity when creating objects . If you don’t know which function to call, you will make mistakes.

5) The compiler will generate a default constructor

If the user does not explicitly implement a constructor, the compiler will generate a default constructor with no parameters.

Compiler-generated default constructor features:

Built-in types are initialized to random values , and custom types call their constructors.

 When the default constructor of class B initializes aa, it will call the constructor of A, and b is initialized to a random value.

6) Initialization list

 Objects of reference and const types must be initialized when they are defined, while the function body of the constructor is an assignment statement and cannot be initialized;

Therefore, the initialization list is introduced, and the initialization work is completed when the object is created.

Initialization list: Starts with a colon, followed by a comma-separated list of data members, each "member variable" followed by
an initial value or expression enclosed in parentheses

 Notice:

Each member can only appear once in the initialization list ;

References and constant objects, custom types that need to pass parameters must be initialized in the initialization list;

The initialization order of the initialization list is only related to the order of class object declaration ;

Second, the destructor

At the end of a class object, the dynamic memory must be released. In order to prevent users from forgetting, a destructor is introduced, which is automatically called at the end of the object's life cycle to complete the memory cleanup of the object.

 Features:
1) Add ~ before the function name  (indicates that it is opposite to the constructor)

2) No parameters, no return value

3) The compiler will generate a default constructor.
Like the constructor, when the user does not explicitly write the destructor, the compiler will automatically generate the destructor

Built-in types are not recycled , and custom types call their destructors.

4) The call sequence is similar to the stack structure, the class is created first , and then the destructor is called.

3. Assignment operator

In order to enhance the readability of the code, C++ introduces the assignment operator =

The assignment operation is for two existing classes;

Keyword: operator = has function parameters and return value

 

characteristic:

1) When the user does not explicitly write, the compiler will generate a default assignment operator, which is characterized by shallow copy

Shallow copy: Copy the target byte by byte to the destination

A shallow copy cannot be used for a copy involving a piece of space : the same piece of space cannot be freed twice in a row ; therefore, user-defined assignment operator overloading is very necessary.

2) The return value is T& , which reduces the copying of passed values ​​and can be copied continuously   

     The parameter type const T&   prevents it from being modified by mistake

3) Hide the first parameter this pointer, and check whether the source of the copy is yourself

3) Cannot be overloaded as a global function

The global function does not have a this pointer, so all parameters need to be passed

4. Copy construction

Copy an existing class to a new class;

which is an overload of the constructor ;

 When passing parameters, parameters must be received by reference ;

Otherwise no reference is taken; infinite recursion occurs

If you want to copy construction, you need to pass parameters. When you pass parameters, you need to call copy construction. When you call copy construction, you need to pass parameters....... Infinite recursion will occur

1) When the user does not display the write, the compiler will generate a default copy constructor, which is a shallow copy;

When dynamic space is involved, a copy constructor must be explicitly implemented, otherwise the same space will be released twice;

2) When the object is returned, it can return a reference, and try to return a reference to improve efficiency;

Distinguish between copy construction and assignment calls

 Copy construction is an existing class object, and assignment means that both classes exist;

———————————————————————————————————————————

This concludes the introduction of C++ default member functions in this issue. There are also two default member functions, but they are basically not used, so I won’t introduce them. It is an important item for constructors. I hope this article will be helpful to everyone!

Guess you like

Origin blog.csdn.net/m0_73299809/article/details/131987899