Inheritance and Dynamic Memory Allocation

How does inheritance interact with dynamic memory allocation (the use of new and delete)? For example, if a base class uses dynamic memory allocation and redefines assignment and a copy constructor, how does that affect the implementation of derived class? The answer depends on the nature of derived class. If the derived class does not itself use dynamic memory allocation, you needn't take any special steps. If the derived class does also use dynamic memory allocation, then there are a couple of new tricks to learn. Let's look at these two cases.

Case 1—Derived Class Doesn't Use new

Suppose we begin with the following base class that uses dynamic memory allocation:

 // Base Class Using DMA class baseDMA ...

Get C++ Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.