I find the Rule of Zero , also mentioned in Peter Sommerlads Slides (p. 32), is very convincing.
Although, it seems, I remember that there was a strict rule: you need to define a virtual descriptor if the class has virtual members and is actually received.
struct Base { virtual void drawYourself(); virtual ~Base() {} }; struct Derived : public Base { virtual void drawYourself(); };
The body of the destructor may even be empty (it only needs an entry in vtbl).
I seem to remember that when using hierarchy
int main() { Base *obj = new Derived{}; obj->drawYourself();
then it is important that delete obj call the correct destructor . Is it correct that if I left the destructor definition completely, it would not become virtual , and therefore the wrong d'tor would be called?
struct Base { virtual void drawYourself();
This leads me to my last question:
- Is the "Rule of Zero" also true in hierarchies with virtual methods.
- or do I need to define a virtual destructor in these cases?
Edit: As I was reminded in response, my version of 1sr had the wrong assumptions. The corresponding (virtual) destructor is in Base , not Derived . But my question is: Do I need to declare (virtual) destructors at all?
c ++ c ++ 11 destructor virtual-destructor rule-of-zero
towi
source share