Should a C++ pure virtual class need a definition?
The first mistake is that you invent terms on behalf of yourself. There is no such thing as "a pure virtual class". There is only "virtual function" and "pure virtual function". Understanding that there is no such thing "pure virtual class" is a key of understanding why this question does not hold.
I used to think that pure virtual class defines an interface (function declaration)
I think C++ is not your first language, but a second language after Java/C#, and you think in Java/C# ideas that has nothing to do with C++.
C++ has interfaces - it's just the declaration of the class:
struct A{
void doNothing();
};
//A.cpp:
void A::doNothing(){
}
This is the interface of struct A. Does it have anything to do with inheritance, virtual functions or polymorphism? No. It's just the declaration of the class, what method and properties exist within it.
Every class needs a valid destructor to allow the program to clean the object resources - memory, etc. It has nothing to do with polymorphism. In your example, A
needs to be destructed somehow. It doesn't matter that B
inherits from it. It must tell the program how to deal with it when it gets out of scope.
As mentioned in the comments, if you only want a virtual destructor (so not UB will be manifested in the case of A* a = new B()
), just declare the destructor as default:
virtual ~A() = default;