我什么时候需要声明我自己的析构函数? [英] When do I need to declare my own destructor?

查看:194
本文介绍了我什么时候需要声明我自己的析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Point 
{
public:
float x,y;
Point(){}
Point(float,float);
点算符+(点);
点运算符*(double);
void rotate_p(float);
void render_p(Point *);
void sub(float);
float get_dist(); // get_distance
};

正如你可以看到,这个类没有指针非静态 em> data-members ,所以我想我可以使用默认析构函数;这是准确的吗?






问题




  • 我需要何时声明我自己的析构函数?


解决方案

>

由于具有自动存储持续时间的数据成员的生命周期与具有它们的实例的生命周期有关,因此不需要明确地调用它们的析构函数;






什么时候通常需要声明我自己的析构函数?

通常,您必须在以下情况下显式声明自己的析构函数:




  • 您正在声明一个类,它应该作为涉及多态性的继承的基础,如果你需要一个虚拟析构函数,以确保 Derived


  • 您需要释放资源,才能通过指向/

    :类有一个文件的句柄,这需要当对象被破坏时被关闭;


  • Exempel 2 :类拥有一个具有动态存储持续时间,因为对象的生命周期可能在类实例被销毁之后很久就存在了,你需要在析构函数中显式地销毁它。


/ li>





隐式生成的析构函数



除非你明确声明你自己的析构函数,否则将由编译器为你创建一个隐式生成的析构函数。


14.4p4 析构 [class.dtor]



< >

如果类没有用户声明的析构函数,析构函数是
隐式声明为默认值(8.4)。隐式声明的
析构函数是其类的内联公共成员。


src:http://www.open-std.org/jtc1/sc22/wg21/docs/papers /2012/n3337.pdf





然而,有一些罕见的/高级的情况下,编译器将拒绝为你生成析构函数,在这些情况下你必须显式声明自己的,或确保析构函数你的类实例从不调用;因为为了销毁对象,析构函数是必需的。


14.4p5 破坏者 [class.dtor]


类X的析构函数定义为delete if:




  • X 具有不重要的
    析构函数的变体成员,


  • 任何非静态数据成员具有类类型 M (或数组
    of)和 M 有一个已删除的析构函数或
    从默认析构函数无法访问的析构函数

    $ b
  • 任何直接或虚拟基本类别都有已删除的析构函式或无法从预设析构函式取得的
    析构函式。


  • 或者,对于虚拟析构函数,查找非数组释放
    函数会导致模糊性或者在被删除的
    或从默认析构函数不可访问的函数中。



src:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf







更多可以通过以下链接阅读:




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:

这篇关于我什么时候需要声明我自己的析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆