C++ empty constructor/destructor questions

Started by
16 comments, last by SiCrane 15 years, 4 months ago
I've got a few questions about empty constructors/destructors when inheritance is concerned. I've googled this, and have read the C++ FAQ Lite entries, but they seem a bit vague on the following questions. I'm hoping someone here will know for sure what the best practice is. 1. I have a non-abstract base class, from which several other classes derive. I do a lot of casing to the base class, and so my base class has a virtual destructor. However, that destructor is empty and unused. My question is, if the base class's destructor is empty, is it still important to define it, just to make sure it is virtual? 2. Same for constructors. If a base class has an empty constructor, does it still need to be defined in the base class? 3. Likewise, if a derived class contains an empty constructor or destructor, do they need to exist just to inherit from the base class, or can either or both be safely left undefined?
Advertisement
Quote:Original post by Nairou
1. I have a non-abstract base class, from which several other classes derive. I do a lot of casing to the base class, and so my base class has a virtual destructor. However, that destructor is empty and unused. My question is, if the base class's destructor is empty, is it still important to define it, just to make sure it is virtual?


Yes - default destructors are non-virtual.

Quote:
2. Same for constructors. If a base class has an empty constructor, does it still need to be defined in the base class?


No.

Quote:
3. Likewise, if a derived class contains an empty constructor or destructor, do they need to exist just to inherit from the base class, or can either or both be safely left undefined?


Both can be left out.
Just as a side note to that, constructors are not inherited. If you want "virtual constructor" behaviour, you are going to have to fake it. (In particular, it is common to use a named virtual member function called 'clone', or something like that, in order to get the effect of a "virtual copy constructor", as the actual copy constructor can't do that. This is typically implemented by redefining it in each class to call the current class' copy constructor. Annoying bit of boilerplate, yes. Sorry.)
Quote:Original post by Driv3MeFar
Quote:Original post by Nairou
Quote:
2. Same for constructors. If a base class has an empty constructor, does it still need to be defined in the base class?


No.
Actually it depends...
If you have non-default constructors, and you delete the default constructor, then a default constructor will not longer be auto-generated, and if you happen to be using the default constructor anywhere then boom - it won't compile.
So in some cases, explicitly defining an empty default constructor can be necessary.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by iMalc
... and you delete the default constructor ...

How would one accomplish that neat trick?

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Nairou
1. I have a non-abstract base class, from which several other classes derive. I do a lot of casing to the base class, and so my base class has a virtual destructor. However, that destructor is empty and unused. My question is, if the base class's destructor is empty, is it still important to define it, just to make sure it is virtual?

Here are some rules about the topic:

A. First and most important, define it, no conditions. Virtual destructor means (for me and at least a few others) "I am a base class".
B. You don't need a virtual empty destructor until you delete a pointer to a base class with different dynamic type, and that dynamic type has some member variables or a non-empty destructor. I.e. "Base* b = new Derived(); delete b;" does not invoke ~Derived() and does not delete the members of Derived, but calls ~Base() and deletes the members of Base (calls their destructors and releases the memory where they were allocated).


Quote:Original post by Nairou
3. Likewise, if a derived class contains an empty constructor or destructor, do they need to exist just to inherit from the base class, or can either or both be safely left undefined?

Constructors and destructors are never undefined. They may be automatically generated. Constructors are never virtual. Destructors must be declared virtual when you want to (safely) inherit from them (see comment above).

Quote:Original post by Nyarlath
A. First and most important, define it, no conditions. Virtual destructor means (for me and at least a few others) "I am a base class".


Erm, well, if you enjoy doing extra work in order to add useless overhead to your code in the case where the base class isn't intended to be used polymorphically, then be my guest. :) (This is also kind of at odds with your point B.)

Anyway, to the OP, I don't really understand how there can be any confusion when it's right there.

Quote:Constructors and destructors are never undefined.


From context, he clearly meant "not explicitly defined". But an argument can be made that for every combination of types T^N that comprise a legal set of parameters for a constructor, every one not supported by the class' defined constructors is "undefined". ;)
Quote:Original post by Zahlman
Erm, well, if you enjoy doing extra work in order to add useless overhead to your code in the case where the base class isn't intended to be used polymorphically, then be my guest.

Erm, I don't call base class something that isn't intended to be used polymorphically. My fault, sorry.
Thanks everyone for your replies!

Quote:Original post by Zahlman
Anyway, to the OP, I don't really understand how there can be any confusion when it's right there.

That is the exact section I was referring to when I said it was a bit vague. That second-to-last paragraph, where it says "if your base class has a virtual destructor, then your destructor is automatically virtual". Seemed like a "duh" statement but still didn't address whether to create the virtual destructor in the first place.
I think its refering to the destructor in the derived class; it doesn't need to be explicitly defined as virtual, its implied from the base class.
Innovation not reiterationIf at any point I look as if I know what I'm doing don't worry it was probably an accident.

This topic is closed to new replies.

Advertisement