Inheritance and dynamic memory allocation in C++

  • 2020-05-27 06:41:28
  • OfStack

Details on inheritance and dynamic memory allocation in C++

How does inheritance interact with dynamic memory allocation? For example, if the base class USES dynamic memory allocation and redefines the assignment and copy constructors, how does this affect the implementation of the derived class? The answer to this question depends on the properties of the derived class. If derived classes also use dynamic memory allocation, you need to learn a few new tricks. Here's a look at the two cases:

1. Derived classes do not use new

Do derived classes need to define destructors, copy constructors, and assignment operators for display?

Don't need to!

First, see if you need a destructor. If a destructor is not defined, the compiler will define a default constructor that does nothing. In fact, the default constructor of a derived class always does something: it executes its own code and calls the base class destructor. Because we assume that members of derived classes do not need to perform any special operations, default destructors are appropriate.

Let's look at the copy constructor. The default copy constructor performs member copy, which is not appropriate for dynamic memory allocation, but is appropriate for members of a new derived class. Therefore, only the objects of the inherited base class need to be considered. Remember that member replication will be replicated according to the data type, so when you copy a class member or an inherited class component, you use the copy constructor for that class. So the default copy constructor for the derived class USES the base class's display copy constructor to copy the base class member part of the derived class object. Therefore, the default copy constructor is appropriate for the new derived class.

The same is true for assignments.

2. Derived classes use new.

If the derived class requires an new operation, you need to show the definition destructor, copy constructor, and assignment operator.

The derived class destructor automatically calls the constructor of the base class, so its own responsibility is to clean up the work performed by the derived class constructor.

Let's look at the copy constructor:


DerivedClass::DerivedClass(const DerivedClass& de):BaseClass(de)
{
//.......
}

Look at the assignment operator:


DerivedClass::operator=(const DerivedClass& de)
{
if(this == &de) return *this;
BaseClass::operator=(de);
//..........
}

In summary, when both base and derived classes use dynamic memory allocation, the destructors, copy constructors, and assignment operators of derived classes must handle base class elements using the desired base class methods. This requirement is met in three different ways.

1. For destructors, this is done automatically;

2. For constructors, this is done by calling the copy constructor of the base class in the initialization member list; If not, the default constructor of the base class is automatically called.

3. For the copy operator, this is done by calling the assignment operator of the base class that is displayed using the scope resolution operator.

The above is C++ inheritance and dynamic memory allocation details, if you have any questions please leave a message or to the site community exchange discussion, thank you for reading, hope to help you, thank you for the support of the site!


Related articles: