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,
1 2 3 4 5 6 7 8 9 10 |
class myclass { public: myclass() { std::cout << "myclass is constructed!\n"; }; }; |
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.
1 2 3 4 5 6 7 8 9 10 |
class myclass { public: ~myclass() // Destructor { }; }; |
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,
1 2 3 |
virtual ~class_name (); |
Virtual destructor declaration example in a class,
1 2 3 4 5 6 7 8 9 10 |
class myclass { public: virtual ~myclass() // Virtual Destructor { }; }; |
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,
1 2 3 |
virtual ~class_name () = 0; |
Virtual destructor declaration example in a class,
1 2 3 4 5 6 7 8 |
class myclass { public: virtual ~myclass() = 0; // Pure Virtual Destructor }; |
An example to Pure Virtual Destructor in C++ is given below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> class my_baseclass { public: virtual ~my_baseclass() = 0; // Pure Virtual Destructor , my_baseclass becomes an abstract class }; my_baseclass::~my_baseclass() // Destructor for the Pure Virtual Destructor { std:: cout << "class object deleted safely\n"; } class my_class : public my_baseclass { public: ~my_class() // Destructor { }; }; int _tmain(int argc, _TCHAR* argv[]) { my_baseclass *test = new my_class; delete test; // Deleted safely by the Virtual Destructor getchar(); return 0; } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition