I do not understand anything, if you make the class destructor pure virtual, then nothing, everything is fine, but as soon as we begin to inherit from this class, there will be an error at the compilation stage, an error of the linker LNK2019 .

It is clear that if you define or mark a destructor as default , then everything is fine, but if I need a pure virtual destructor?

https://ideone.com/F0h0do

  • one
    A destructor cannot be purely virtual, it must have an implementation, since it will in any case be called by destructors of classes inheriting from a given one - VTT
  • @VTT: Ahhh, here is a pancake, I haven't looked at the assembler code, there are absolutely no definitions for destructors if you make it purely virtual. Add as an answer. - LLENN
  • @VTT: The destructor really needs to have an implementation, but it nevertheless does not prevent it from being a pure virtual function. At least within the formal terminology of C ++. - AnT
  • What is the terminology: "pure virtual"? No one will understand you, and the question in this vein is useless. There is a concept - abstract , and it should be used. Speak Russian. - mega
  • @mega, literal translation pure virtual. I do not understand what embarrassed you. - yrHeTaTeJlb

1 answer 1

Even when you make a destructor a pure virtual function, you still need to provide a definition for that destructor.

Pure virtual functions cannot be called through the mechanism of a virtual (dynamic) call, but this does not in any way prohibit calling the same functions through the mechanism of an ordinary direct non-virtual (static) call. Therefore, if you have non-virtual calls of a certain pure virtual function in the program, then you will have to provide a definition for this function.

A destructor is just an example of a function that, in general, will be implicitly called in a non-virtual way too. In particular, the base class destructor will be implicitly called from the destructors of the heir classes, if any. In your case, the error just arises as soon as the heir classes appear in the program. Therefore, a definition should always be provided for the destructor.

It is also worth noting that in C ++ the definitions of pure virtual functions should be made outside the class definition , i.e. the grammar of the language does not allow "thrust" into the declaration of the function at the same time and the pure specifier = 0 and the body of the function. The definition should be done separately.