7

I wonder if it's a good practice to always write constructor/destructor even if I don't use them.

class Foo
{
public:
    Foo(){};
    ~Foo(){};
};

Or is it a better practice to write them only when I actually use them?

class Foo
{
public:
};
Zack Lee
  • 2,784
  • 6
  • 35
  • 77
  • 1
    Note that declaring a virtual destructor will force each object of your class to contain a vtable-pointer, increasing its memory usage by one word (e.g. 8 bytes) per `Foo` that is created. So if you know that your `Foo` class will never use virtual methods (and/or will never be inherited by a subclass that uses virtual methods), then there is an advantage to not declaring a virtual destructor (declaring a non-virtual destructor would not increase memory usage though). OTOH if you know the object will be using virtual methods, then declaring a virtual destructor is a good idea. – Jeremy Friesner Aug 27 '18 at 01:27
  • no reason writing constructor, and in particular `vritual` destructor in this case. Please think of some more real-life example. – Serge Aug 27 '18 at 01:29
  • "use them" seems to have a very different meaning to you than the way C++ is discussed by experts (and in the Standard). In common parlance, "using" a function (including a special member function) happens if you call it or take its address. You seem to mean when you write something in the function body. – Ben Voigt Aug 27 '18 at 01:36
  • 2
    Stack overflow isn't for questions of opinion. – xaxxon Aug 27 '18 at 01:49
  • 3
    @xaxxon There are differences between "primarily opinion-based question" and "question whose answer will contain some opinions". "Primarily" is the keyword. –  Aug 27 '18 at 01:57
  • @NickyC whether you should **always** do it, is going to be primarily opinion and that's what the question asks. – xaxxon Aug 27 '18 at 01:58
  • 4
    @xaxxon But the question isn't *asking* for opinions, and the OP may not realize that it's a matter of opinion. A reasonable answer here could be "That's a matter of opinion; some people do it because ___, while others think it's unnecessary because ___." A lot of good and valid questions are like this. The aim of the guideline is to stop questions from turning into debates. – Caleb Aug 27 '18 at 02:32
  • 1
    @caleb The question asks for "better practice" without qualifying it. That's an opinion. – xaxxon Aug 27 '18 at 02:32
  • 2
    @xaxxon: It's not an open-ended request for the best practice, it presents exactly two options and asks which *of those two* is better. A question that has fact-based answers. Just because you thought of an opinion-based answer does not make all other answers, or the question itself, opinion-based. – Ben Voigt Aug 27 '18 at 13:12

1 Answers1

16

It's a bad idea to user-define special member functions when the default ones are sufficient.

  • You will lose the default-generated move operations, see Does a default virtual destructor prevent compiler-generated move operations?

  • Your class will no longer be trivial, causing allocation and deallocation and containers holding your class to become much less efficient.

  • The defaulted definitions may be automatically noexcept, but you lost that.

  • Your class is no longer an aggregate, so you can't use aggregate initialization.

  • If you make the destructor virtual, as shown in your question, you also lose standard-layout.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Also, depending on the ABI trivially copyable type can be passed to function more efficiently (passed in a register rather than on the stack). – Oliv Aug 27 '18 at 09:47
  • It is not. I am not talking about the c++ language specification but calling conventions as described for exemple in the itanium c++ ABI. – Oliv Aug 28 '18 at 07:59
  • @Oliv: The rules of the specification restrict the calling conventions. A variable can live in a register and have no memory address only if both the specification and the ABI permit it. – Ben Voigt Aug 28 '18 at 12:31