Jump to content
  • Advertisement
Sign in to follow this  
Nairou

C++ empty constructor/destructor questions

This topic is 3541 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
... and you delete the default constructor ...

How would one accomplish that neat trick?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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". ;)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!