Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to declare my own destructor?

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?
like image 803
0ctoDragon Avatar asked Jun 11 '14 16:06

0ctoDragon


1 Answers

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
like image 119
Filip Roséen - refp Avatar answered Oct 06 '22 15:10

Filip Roséen - refp