1

This is probably a simple question, but: If a program uses the delete operator, is a destructor not needed? This is in "C++".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
The10thDoctor
  • 125
  • 1
  • 13

1 Answers1

3

The delete operator is not a substitute for a destructor...it will cause the destructor to be invoked.

The compiler will supply a default destructor, if you do not define one yourself. Whether the default destructor is sufficient, or whether you need to supply your own, is a completely separate issue from whether you use the delete operator explicitly, or merely allow the object to go out of scope.

Edit: Since Michael Dorgan mentioned it, I might as well add this:

Rule of Three

The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class defines one of the following it should probably explicitly define all three:

destructor
copy constructor
copy assignment operator
Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • It's several years too late to be teaching the Rule of Three. http://stackoverflow.com/q/4782757/103167 – Ben Voigt Jun 17 '13 at 23:22
  • Perhaps, but not completely and I will also add that many locations are not allowed to code in C++11 yet so it still has much value. – Michael Dorgan Jun 18 '13 at 21:26