When do I need to declare my own destructor?

2020-02-27 11:20发布

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?

6条回答
beautiful°
2楼-- · 2020-02-27 11:30

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)

查看更多
beautiful°
3楼-- · 2020-02-27 11:30

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

  1. Deallocate all the memory the objects currently own (note that it might have allocated memory but transferred its ownership).
  2. Release all the resources the object has acquired.
  3. ....

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.

查看更多
放荡不羁爱自由
4楼-- · 2020-02-27 11:35

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/

查看更多
冷血范
5楼-- · 2020-02-27 11:36
  • 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.

查看更多
Bombasti
6楼-- · 2020-02-27 11:42

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.

查看更多
够拽才男人
7楼-- · 2020-02-27 11:50

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:

查看更多
登录 后发表回答