Deleting a derived class object using a pointer of base class type that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor. For example, following program results in undefined behavior.
// CPP program without virtual destructor // causing undefined behavior #include<iostream> using namespace std; class base { public: base() { cout<<"Constructing base \n"; } ~base() { cout<<"Destructing base \n"; } }; class derived: public base { public: derived() { cout<<"Constructing derived \n"; } ~derived() { cout<<"Destructing derived \n"; } }; int main(void) { derived *d = new derived(); base *b = d; delete b; getchar(); return 0; } |
Although the output of following program may be different on different compilers, when compiled using Dev-CPP, it prints following:
Constructing base Constructing derived Destructing base
Making base class destructor virtual guarantees that the object of derived class is destructed properly, i.e., both base class and derived class destructors are called. For example,
// A program with virtual destructor #include<iostream> using namespace std; class base { public: base() { cout<<"Constructing base \n"; } virtual ~base() { cout<<"Destructing base \n"; } }; class derived: public base { public: derived() { cout<<"Constructing derived \n"; } ~derived() { cout<<"Destructing derived \n"; } }; int main(void) { derived *d = new derived(); base *b = d; delete b; getchar(); return 0; } |
Output:
Constructing base Constructing derived Destructing derived Destructing base
As a guideline, any time you have a virtual function in a class, you should immediately add a virtual destructor (even if it does nothing). This way, you ensure against any surprises later.
Reference: Secure Coding
This article is contributed by Rahul Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
Recommended Posts:
- Pure virtual destructor in C++
- Calling virtual methods in constructor/destructor in C++
- Private Destructor
- Is it possible to call constructor and destructor explicitly?
- __attribute__((constructor)) and __attribute__((destructor)) syntaxes in C
- Order of Constructor/ Destructor Call in C++
- Difference Between Constructor and Destructor in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- Difference between Virtual function and Pure virtual function in C++
- Can static functions be virtual in C++?
- Virtual functions in derived classes
- Advanced C++ | Virtual Constructor
- Advanced C++ | Virtual Copy Constructor
- Default arguments and virtual function
- Can virtual functions be inlined?
- Pure Virtual Functions and Abstract Classes in C++
- Virtual Function in C++
- Virtual destruction using shared_ptr in C++
- C++ interview questions on virtual function and abstract class
- Virtual base class in C++
Improved By : linhdh

