Can a destructor be pure virtual in C++?
Yes, it is possible to have pure virtual destructor in C++. Pure virtual destructor are legal in standard C++ and one of the most important thing is that if class contains pure virtual destructor it is must to provide a function body for the pure virtual destructor. This seems strange that how a virtual function is pure if it requires a function body? But destructors are always called in the reverse order of the class derivation. That means derived class destructor will be invoked first & then base class destructor will be called. If definition for the pure virtual destructor is not provided then what function body will be called during object destruction? Therefore compiler & linker enforce existence of function body for pure virtual destructor.
Take this example:
#include <iostream>
class Base
{
public:
virtual ~Base()=0; // Pure virtual destructor
};
class Derived : public Base
{
public:
~Derived()
{
std::cout << "~Derived() is executed";
}
};
int main()
{
Base *b=new Derived();
delete b;
return 0;
}
The linker will produce following error in the above program.
test.cpp:(.text$_ZN7DerivedD1Ev[__ZN7DerivedD1Ev]+0x4c): undefined reference to `Base::~Base()'
Now if the definition for the pure virtual destructor is provided then the program compiles & runs fine.
#include <iostream>
class Base
{
public:
virtual ~Base()=0; // Pure virtual destructor
};
Base::~Base()
{
std::cout << "Pure virtual destructor is called";
}
class Derived : public Base
{
public:
~Derived()
{
std::cout << "~Derived() is executedn";
}
};
int main()
{
Base *b = new Derived();
delete b;
return 0;
}
Output:
~Derived() is executed
Pure virtual destructor is called
<
div>
It is important to note that class becomes abstract class when it contains pure virtual destructor. For example try to compile the below program.
#include <iostream>
class Test
{
public:
virtual ~Test()=0; // Test now becomes abstract class
};
Test::~Test() { }
int main()
{
Test p;
Test* t1 = new Test;
return 0;
}