[C++] default member function

foreword

1. What is a default member function

Second, the constructor

3. Destructor

4. Copy construction

5. Assignment operator overloading

Six, const members

Seven, & operator overloading (understanding)

Summarize:


foreword

  • This section mainly talks about four default member functions and operator overloading,
  • Review the usage of this pointer
  • Understanding Shallow Copy

1. What is a default member function

  • The default member function, generated in the class, has the functions of initialization, cleaning, and copying .
  • In a class, we can implement a default member function (written by ourselves), and if we don’t write this function, the compiler will automatically implement it, so it is called a default member function

Now we can understand that if a class is empty then it also contains default member functions

Second, the constructor

After a class is instantiated, the class should be initialized

The initialization function provided by C++ is called a constructor. After the class is instantiated, the constructor is automatically called to complete the initialization work.

1. No parameter constructor

  •  There are no parameters, so in the constructor, we have to initialize each data
  • Calling the no-argument constructor is the instantiation of the class, because the constructor is automatically called when it is created

2. Constructor with parameters

  •  Constructors can be overloaded, so when we write constructors, we generally overload parameters
  • When calling, add () after instantiation

3. Automatic constructor (generally not used)

In the class, if there is no constructor, the compiler will automatically generate the constructor after instantiation

Generated constructor, initialized to random values

If there is nesting in the class , and the nested class has a constructor, then the nested class will call the constructor initialized by itself

important point:

  • In general, use a parameterized constructor with overloading
  • no return value


3. Destructor

  • At the end of the object life cycle, it is automatically called to complete the cleanup of the class

1. Definition

  • ~+ __(class name)
  • no parameters, no return value

  •  The call of the destructor is mainly to prevent memory leaks, such as stacks, queues, linked lists, etc.

But there are also many that do not need to be written, and can be automatically generated by the compiler

2. Features

  • A class has one and only one destructor
  • There are custom members in the class, and the class will also call the destructor of the custom members


4. Copy construction

Used to copy the created class to get a new class

1. Definition:

  • Class name + (const class name & variable) parameters should be received by reference, otherwise it will cause infinite recursion

Reasons: 1. You need to open a space first. 2. To open a space, you need to call the structure, and then call the copy structure again, and it will continue recursively.

2. Features:

  • Copy construction is an overload of the constructor
  • This implies the this pointer
  • Do not write a copy constructor, the compiler will automatically generate a copy constructor   

Copy objects byte by byte This method is a shallow copy. The copy function generated by the compiler will satisfy most environments, but it is not applicable when it involves dynamic memory, memory release, etc.

Reason: Dynamic memory generally stores pointers, so when copying, the two data point to the same space. Once the life cycle of a data ends, the memory will be released and the pointer will be empty. The same memory can only be released once, so the program will run. crash


5. Assignment operator overloading

1、operator

For built-in types, there are operations such as (+, -, *, =, ==), etc.

Operator overloading for custom operators 

definition:

  • return value type operator operator (parameter relationship)
  • The formal parameter appears to be one member less than the actual number of operations

Reason: There is this pointer here, such as s1=s2, then the parameter is (const class name formal parameter name) representing s2, and s1 is represented by this pointer.

Notice:

  •  Cannot be other operators such as @
  • It cannot be .* :: sizeof ? : . and other five types

 2. Assignment operator

 important point:

  • return type
  • return is *this
  • The compiler will automatically generate an assignment operation, copying by byte

Six, const members

  • The function member of the const modified class means that the class members of the function cannot be modified in any way
  • which implies the this pointer

Generally speaking, for objects whose value does not change, add const at the end of the function definition or life

This involves the enlargement and reduction of permissions. The permissions can be reduced, but not enlarged.

Consider the following topics:

  • Can a const object call a non-const member function? No, the authority is enlarged
  • Can a non-const object call a const member function? Yes, reduced permissions
  • Can const member functions call other non-const member functions? No, the authority is enlarged
  • Can non-const member functions call other const member functions? Yes, reduced permissions

Seven, & operator overloading (understanding)

This operator generally does not need to be written, the compiler will automatically generate it, and in most cases, it can be applied.


Summarize:

This section mainly talks about the usage and characteristics of several member functions

  • In general, the constructor must have, destructor, copy assignment can be used in some cases by the compiler
  • Shallow copy means that the compiler automatically generates copy construction and operator= copy by byte

——— There are no shortcuts in programming.

Guess you like

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