Details on when C++ needs to define the assignment and copy constructor

  • 2020-05-10 18:35:19
  • OfStack

Inheritance and dynamic memory allocation

If the base class USES dynamic memory allocation and defines destructors, copy constructors, and assignment functions, but dynamic memory allocation is not used in derived classes, then there is no need to display the definition destructors, copy constructors, and assignment functions in derived classes.

When a base class and a derived class use dynamic memory allocation, the destructor, copy constructor, and assignment operator of the derived class must use the corresponding base class method to handle the base class element. This requirement is met in three different ways. For the destructor. This is done automatically, which means that there is no need to display the destructor of the calling base class in the destructor of the derived class. For constructors, this is done by calling the base class's copy constructor in the initialization member list, or if not, automatically calling the base class's default constructor, and for assignment operators, this is done by calling the base class's assignment operator explicitly using the domain resolution operator.

Member functions generated by the compiler

1.   default constructor

The default construct hash either has no arguments, or all arguments have default values. If no constructor is defined, the compiler defines the constructor. In addition, if the member initialization list of the derived class constructor does not show a call to the base class constructor, the compiler USES the default constructor of the base class to construct the base class part of the derived class object. In this case, if the base class does not have a constructor, it will result in a compile-stage error. If a constructor is defined, the compiler will not define the default constructor. In this case, if you need a default constructor, you must provide it yourself. (the best advice is that once a constructor is defined, it is best to define a default constructor, which can be easily invoked in other subclasses.)

One of the motivations for providing constructors is to ensure that objects are always properly initialized. Also, if the class contains pointer members, they must be initialized. Therefore, it is best to provide a display default constructor that initializes all class data members to reasonable values.

2.   copy constructor

The copy constructor takes the object of its class as an argument. The copy constructor is used in the following cases

Initializes the new object as a homogeneous object
Pass the object by value to the function
The function returns the object by value
The compiler generates temporary objects

If the program does not use a copy constructor, the compiler will provide the prototype but not the function definition; otherwise, the program will define an assignment constructor that performs member initialization. That is, each member of the new object is initialized to the value of the corresponding member of the original object. If the member is a class object, the copy constructor of the corresponding class is used when initializing the member.

If the member pointer is initialized with new, it is usually required to perform a deep copy, or the class may contain static variables that need to be modified. In this case, you need to define your own copy constructor.

3.   assignment constructor

The default assignment operator is used to handle assignments between objects of the same class. Do not confuse assignment with initialization. If the statement creates a new object, initialization is used, and if the statement modifies the value of an existing object, assignment is used.

The default assignment is member assignment. If the member is a class object, the default member assignment will use the assignment operator of the corresponding class. If you need to show the defined copy constructor, for the same reason. You also need to display the definition assignment operator.
 
For derived classes, protecting members is similar to common members, but externally, protecting members is similar to private members. Derived classes can access the protected members of the base class directly, but private members can only be accessed through the member functions of the base class.
 
The destructor of the base class should be virtual. Thus, when a derived object is deleted by a base class pointer or reference to an object, the program first calls the destructor of the derived class and then the destructor of the base class, not just the destructor of the base class

How to determine the true type of object to which a pointer is pointing, using the runtime mechanism in C++, typeid can achieve the target


Related articles: