C++C++11C++14C++17Introduction to C++Learn C++

Learn About Pure Virtual Destructors In C++ App Classes

If you construct an object, sometimes you need operations to deconstruct. Destructors not only used in classes but also used with struct and union data types. Do you want to learn what a Pure Virtual Destructor is in a C++ App, or what kind of methods we have that we can declare and use a pure virtual destructor? In this post, we will try to explain how to use Pure Virtual Destructor in C++ Classes with some short examples.

The Constructor in C++ is a function, a method in the class, but it is a ‘special method’ that is automatically called when an object of a class is created. We don’t need to call this function. Whenever a new object of a class is created, the Constructor allows the class to initialize member variables or allocate storage. This is why the name Constructor is given to this special method. Here is a simple constructor class example below,

The Destructor in classes (i.e class_name) is a special member function that occurs when we delete objects, in other words it is called when the lifetime of an object ends. The purpose of the destructor is to perform operations like tidying up memory and notifying things when the object is destroyed. The object may have acquired or allocated data on memory during runtime. If so, they need to be freed too when objects are being deleted otherwise, we could ‘leak’ memory – where memory is allocated but can’t be used or deallocated since the owner object is defunct. The destructor is the function that frees the resources of the object such as that memory and related handles. When we construct an object, sometimes we need operations to deconstruct. Destructors are not only used in classes but also used with struct and union data types.

Pure Virtual Destructor

A Virtual Destructor is usually required in a base class, and it is used by the class which has this base class. Note that, deleting a class object through a pointer to base invokes undefined behavior unless the destructor in the base class is virtual:

Syntax,

Virtual destructor declaration example in a class,

A Pure Virtual Destructor is defined in a base class with =0; statement, and its body is declared outside of the class. Pure Virtual Destructors are mostly used in C++ standard and it is generally used in libraries (dynamic DLL or static)although there are use cases for virtual destructors. Note that, if your class contains a pure virtual destructor, it should provide a function body for the pure virtual destructor. If you are asking why a pure virtual function requires a function body the answer is destructors are not actually overridden, rather they are always called in the reverse order of the class derivation. That means, firstly a derived class destructor will be invoked, and then the base class destructor will be called. If the definition of the pure virtual destructor is not provided, then the compiler and linker enforce the existence of a function body for pure virtual destructors.

Syntax,

Virtual destructor declaration example in a class,

An example to Pure Virtual Destructor in C++ is given below,

Note that a class becomes an Abstract Class when it contains a Pure Virtual Destructor and abstract classes are used often in professional applications.

You can download a free trial of C++ Builder and try this code today.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

Worth reading...
How To Force A Class Destructor In C++ Software