When do I need to declare my own destructor?

前端 未结 6 834
时光取名叫无心
时光取名叫无心 2020-12-31 05:00
class Point    
{
public:
    float x,y;
    Point() {}
    Point(float,float);
    Point operator + (Point);
    Point operator * (double);
    void rotate_p(float)         


        
6条回答
  •  温柔的废话
    2020-12-31 05:40

    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

提交回复
热议问题