3

If no user-defined destructor exists for a class and one is needed, the compiler implicitly declare a destructor. When I need to declare my own destructor?

iammilind
  • 68,093
  • 33
  • 169
  • 336
Aan
  • 12,247
  • 36
  • 89
  • 150

4 Answers4

5

When the implicitly declared destructor won't do what you need it to.

This is somewhat involved. You should look up and research the rule of three.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 9
    Rather than posting a link to Google, you should post a link to [this excellent SO-question](http://stackoverflow.com/questions/4172722) covering the topic in great detail. – Björn Pollex Oct 24 '11 at 08:48
  • It's #4 in the search results when I do it. :) – Karl Knechtel Oct 24 '11 at 08:49
  • Also, a topic not covered in there is that you need to declare an empty virtual destructor if you want a polymorphic class. – Björn Pollex Oct 24 '11 at 08:50
  • 3
    I agree about the rule of three in general, but it's worth mentioning that a lot of cases where one would have provided a user defined destructor and assignment operator in the past are eliminated by an intelligent use of RAII classes as members. – James Kanze Oct 24 '11 at 09:12
  • @James very true, definitely shouldn't be overlooked. – Karl Knechtel Oct 24 '11 at 09:13
  • 1
    @James: I think one can even say, as a design principle, "all cases" rather than just "a lot of cases". That is, only RAII classes should have destructors, and a RAII class should manage the lifetime of exactly one resource and do nothing else. – Steve Jessop Oct 24 '11 at 09:46
  • @SteveJessop Perhaps if you stretch the meaning of RAII class enough. I can think of a number of cases which I wouldn't consider an RAII class, but the class does manage a resource: something like `boost::mutex`, for example, or `std::filebuf`. The rule of three isn't appropriate in such cases, either, since they normally won't support assignment. Which makes me wonder if there are cases where the rule of three is appropriate (other than `shared_ptr` and `std::vector`). – James Kanze Oct 24 '11 at 10:44
  • @James: possibly it is a stretch, but I would call those RAII classes. They acquire a system resource on initialization, they release it on de-initialization, that's certainly good enough for me. And I'd say that some RAII classes are copyable where others aren't. The ones that aren't only have to implement 1 out of the rule of 3, but that's because they mark 2 of them private, so they don't ignore them entirely. – Steve Jessop Oct 24 '11 at 10:51
  • @SteveJessop They certainly use techniques common to RAII. (Or maybe not---the truly RAII classes I know don't acquire the resources themselves, despite the name.) But they do a lot more. A class like `boost::mutex` acquires the resource because it needs it for its own defined semantics, not because someone else needs it and wants it managed. It's a question of vocabulary, and there is no official definition, but to me, it seems like calling `boost::mutex` and RAII class doesn't help clarity. – James Kanze Oct 24 '11 at 12:35
  • @James: I think of RAII as a property of an interface -- using object lifetime to avoid an explicit resource release call. If you prefer to define it as, "I will manage some resources for you" then one of us would have to pick a different word for the other person's thing, since they're both important. I suspect that I cast the term a bit too widely compared with common usage, and you cast it narrowly. Certainly a LockSession object (a) must be true RAII, it's a standard example, and (b) acquires the resource itself, by calling an acquire-type function on a lock object. – Steve Jessop Oct 24 '11 at 13:44
  • @SteveJessop "I suspect that I cast the term a bit too widely compared with common usage, and you cast it narrowly." Probably. Your example of `scoped_lock` (I think that's what you mean by `LockSession`) certainly shows that I'm using it more narrowly than most. But I don't think many people think of `std::filebuf` as an RAII class either. I don't really know where to draw the line. – James Kanze Oct 24 '11 at 13:57
  • @James: `scoped_lock` is an example of what I mean by LockSession, I just didn't name it because it's not specifically Boost's API that's the classic demonstration of what's so good about RAII, it's the general notion of defining the period holding a lock as the lifetime of an object. – Steve Jessop Oct 24 '11 at 14:22
1

If you allocate memory, create objects, or anything else you do, either in the constructor or afterwards, that needs to be cleaned up when your object is destructed.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
1

When you want to ensure something is finished. The 'something' would usually be closing a network connection or file or freeing up some memory etc...

gef
  • 7,025
  • 4
  • 41
  • 48
1

Each time you must execute special tasks on object destruction, i.e: memory deallocation, close network connections, decrement count references, threads synchronization, throw stored exceptions, etc.

Tio Pepe
  • 3,071
  • 1
  • 17
  • 22
  • "throw stored exceptions" - a warning though, throwing from a destructor isn't something you want to do lightly! It's pretty difficult to avoid situations where your program will `terminate` without telling you anything useful, although there are limited cases where it's possible. – Steve Jessop Oct 24 '11 at 09:48