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.

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Join Two Wide Strings In Modern C++ Software

C++C++11C++14C++17Learn C++

How To Use The All-Powerful swscanf function In Your C++ App

C++ComponentsLanguage FeatureLearn C++

How To Use A Sharpen Effect in C+ Software On Windows?

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

What Is The Erase WString Method In A Modern C++ App

Worth reading...
What You Need To Know About Virtual Destructors In C++ Apps