Pure virtual function

I want an example of the destructive function as a pure virtual function


Please help me
You can't have a pure virtual destructor. If you did how would you delete the memory allocated on the heap in such a class? You can have a class with a pure virtual function which cannot then be instanted directly but yet its destructor is called so that any memory allocated on the heap within that class can be deleted.
Pure virtual destructors are useful when:
1. you need an abstract base class (so that some member function must be pure virtual)
2. you plan to manage dynamic lifetimes through pointers to this base (so the destructor must be virtual)
3. there are no meaningful member functions in this class that can be made virtual

Remember that although the body of a pure virtual function is usually optional, it is required for the destructors:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <memory>
struct Base {
   virtual ~Base() = 0;
};
Base::~Base() {}
struct Derived : Base {
};
int main()
{
   std::unique_ptr<Base> ptr(new Derived);
}
Last edited on
Topic archived. No new replies allowed.