+ 6

Importance of pure virtual destructor

As we know that pure virtual is nothing but a function without definition and same need to be defined in derived classes. destructor can also be pure virtual, but compiler insist to have body of pure virtual destructor.. why so , because resources of base should be freed by base destructor.. with this, only advantage of pure virtual destructor is that it makes class as abstract class ... is this the only benefit of pure virtual destructor or something else is also there ?

15th Sep 2018, 3:46 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Respostas
+ 3
Pure virtual destructor could have a body. The cpp file that defines it could also defines destructor of inherited classes. All other cpp file generate compilation error in case of inheritance. It could be used to restrict interface inheritance outside the library. But I completely agree with you that pure virtual destructor is useless feature.
15th Sep 2018, 5:12 PM
Sergey Ushakov
Sergey Ushakov - avatar
+ 3
The only use case for a pure virtual destructor I can come up with is if you want an object to be abstract without forcing to override any method for some reason. Another reason could be so that they wouldn't need to add an exception into the language for the destructor. The pure virtual destructor needs a body because, if there was none, what should it call when it's time to destruct the object?
15th Sep 2018, 4:22 PM
Dennis
Dennis - avatar
+ 3
agree that compiler allow to have a body of pure virtual function also... but if you define body for that function, you are unnecessary wasting your time... isn't it? pure virtual means you are not allowed to create object and that pure virtually defined method never gets called... something which is available or allowed doesn't mean we should use it.... this is what I know... correct me if I have misunderstood something to conclude : I don't see advantage of pure virtual destructor... virtual destructor is okay... isn't it safe to assume that pure virtual destructor is just the facility and don't have practical usecase... just to make abstract class, use propee method as abstract rather than making destructor aa pure virtual
16th Sep 2018, 6:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Dennis I am sorry but I couldn't get your point starting with another reason about exception.. pure virtual destructor is already exception as being pure virtual also, compiler ask to have body for the same
16th Sep 2018, 4:17 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
I completely agree with Dennis . Each pure virtual function can have a body. In case of pure virtual destructor it must have one. Nevertheless there is in fact a different btw. virtual functions and virtual destructors: Virtual destructors automatically calls the destructor of the super class, no other functions are working this way. Anyhow, it makes sense.
16th Sep 2018, 5:19 PM
K Locher
K Locher - avatar
+ 1
@Ketan Lalcheta Not providing a body for a pure virtual destructor is not a compile error, the program can be translated into object code, even without a body for it. Instead, the problem occurs during linking. e.g. some functions/library cannot be found. So there is no exception build into the language that disallows the existence of a pure virtual destructor. Since simply allowing the existence causes no ill-effects anyway, there is no need to explicitly disallow it. That's what I meant.
16th Sep 2018, 10:26 AM
Dennis
Dennis - avatar
+ 1
Ketan Lalcheta I agree pure virtual destructor is a matter of taste; there are other solutions creating an abstract class. Anyhow, providing a pure virtual function with a body is useful if you want to enforce a derived class to implement this function BUT want also provide a default implementation to be called from the function in the derived class. Of couse, there are also other solutions getting this, but it is clear and elegant, isn’t it?
28th Sep 2018, 11:14 AM
K Locher
K Locher - avatar