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?
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)
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
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.
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/
Although if you don't define one, the language will define a destructor for you.
Generally, is a good idea to define your destructor as
virtual
, in order to assure that things will be OK if someone inherits from your class.However, it is mandatory to define your destructor if you allocate dynamically memory in your constructors, and make sure that any allocated memory will be deleted in it, in order to avoid memory leaks.
Another suggestion is that if your compiler supports C++11 features, it would be for the best to avoid raw pointers for your memory allocations and use smart pointers (i.e., RAII). Thus, you will not have to delete any previously allocated memory in your destructors.
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.
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.
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.
More can be read under the following link: