class Point
{
public:
float x,y;
Point() {}
Point(float,float);
Point operator + (Point);
Point operator * (double);
void rotate_p(float);
void render_p(Point*);
void sub(float);
float get_dist();//get_distance
};
As you can see this class has no pointers as non-static data-members, so I think I can just use the default destructor; is this accurate?
Question
- When do I need to declare my own destructor?
Introduction
Since data-members with automatic-storage duration have their lifetime bound to that of the instance having them, there's no need for you to call their destructor explicitly; they will always be destroyed whenever the instance of the class is.
When do I normally need to declare my own destructor?
Normally you will have to explicitly declare your own destructor if:
You are declaring a class which is supposed to serve as a base for inheritance involving polymorphism, if you do you'll need a virtual destructor to make sure that the destructor of a Derived class is called upon destroying it through a pointer/reference to Base.
You need to release resourced aquired by the class during its leftime
Example 1: The class has a handle to a file, this needs to be closed when the object destructs; the destructor is the perfect location.
Exempel 2: The class owns an object with dynamic-storage duration, since the lifetime of the object can potentially live on long after the class instance has been destroyed you'll need to explicitly destroy it in the destructor.
The implicitly generated destructor
Unless you explicitly declare your own destructor, an implicitly generated destructor will be created for you by the compiler.
14.4p4
Destructors [class.dtor]
If a class has no user-declared destructor, a destructor is
implicitly declares as defaulted (8.4). An implicitly declared
destructor is an inline public member of its class.
src: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
There are however some rare/advanced cases where the compiler will refuse to generate a destructor for you, under these cases you must explicitly declare your own, or make sure that the destructor for your class instance is never called; because in order to destroy an object, a destructor is neccessary.
14.4p5
Destructors [class.dtor]
A default destructor for a class X is defined as delete if:
X is a union-like class that has a variant member with a non-trivial
destructor,
any of the non-static data members has class type M (or array
thereof) and M has a deleted destructor or a destructor that is
inaccessible from the default destructor,
any direct or virtual base class has a deleted destructor or a
destructor that is inaccessible from the default destructor,
or, for a virtual destructor, lookup of the non-array deallocation
function results in an ambiguity or in a function that is deleted
or inaccessible from the default destructor.
src: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
More can be read under the following link:
- cppreference.com - Destructors
You might also want to look at the "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,
Wikipedia Link: http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)
Destructor's main purpose is to release used memory from heap or to delete links to other objects, etc. You do not allocate memory from heap here, you do not make any relations between objects, so no destructor is needed.
When memory is accounted for at compile-time (i.e. no memory is allocated at run-time), then a destructor CAN be created, but this is not necessary. In such instances, a destructor for the class is implied by the class' definition; a destructor will be supplied for you behind the scenes, and memory will be freed when instances of the class go out of scope.
EDIT: a simple, handy tutorial can be found here - http://www.learncpp.com/cpp-tutorial/86-destructors/
A destructor is code that is executed when an object is destroyed... period.
What goes in the destructor depends on the semantics associated to the object.
Typical stuff that go in the destructor (but might go elsewhere, e.g. think of a pooled object) are
- Deallocate all the memory the objects currently own (note that it might have allocated memory but transferred its ownership).
- Release all the resources the object has acquired.
- ....
The exception is that you must declare a virtual destructor, even if empty when in a hierachy of classes, you would need to be able destroy objects of child classes from a pointer to the parent class.
The compiler will provide a default destructor for you if you don't define one.
In addition, attributes contained within the class will get their destructor called automatically after the (implicit/explicit) class destructor is run.