Did you know that a destructor can be pure virtual? It can be defined as such, but it still needs a body declaration. It is a way of making a class abstract (instances of it cannot be created) without having to make any pure virtual methods. So use it if you have a base class that you don’t want users to instantiate but you have no other candidates for pure virtual specifier 🙂
Additional discussion of this topic at Geeks for Geeks: Pure virtual destructor in C++.
Complete listing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class base { public: virtual ~base() = 0; }; base::~base() {} class derived : public base { public: virtual ~derived() {} }; int main(int argc, char** argv) { //base b; // Compile error: Variable type 'base' is an abstract class derived d; return 1; } |
yea but why I’d need a pure virtual destructor? I think you should invest more effort on the why side of your posts.
This represent an example of good and short explanation 🙂
https://www.geeksforgeeks.org/pure-virtual-destructor-c/
I think I explained why you would need it in my last sentence 😉 Thanks for the link; I’ll update the post.