Skip to main content
gamedev.net
🔒 Locked

Destructors

Started by Caereth Sep 27, 2000 at 4:19 PM 2 replies 300+ views
Original Post
Caereth
Caereth
When to use them? What is to be done in them? Are they needed? thanks
Wilka
Wilka
> When to use them?

Whenever you need a class to do some clean up work, e.g. freeing resources.

> What is to be done in them?

Clean up code, e.g. freeing resources.

> Are they needed?

Yes.

If you classes need to any allocation in the constructor, you should do the de-allocation in the constructor.
Lethe
Lethe
1.Whenever the class could get deleted and lose pointers to data, inturn making a memory leek, or wants to adjust some external stat related to the data stored in the class.
2.Delete allocated memory. Possible do things to other classes, like adjusting the next/last pointers in a linked list, or reducing an entity count etc.
3.Yes. OOP would become a real pain without them.
-Lethe
Wilka
Wilka
quote:

3.Yes. OOP would become a real pain without them.



And using exceptions safely would be far to much hassle, and the same goes for functions with more than one return point. It''s so bad in java because it has garbage collection, but without things like std::auto_ptr a lot of C++ programs would be filled with memory leaks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.