什么是默认析构函数? [英] What are defaulted destructors used for?

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

问题描述

我可以理解默认的构造函数,因为用户定义的构造函数将禁用编译器生成的,使对象不可复制等。

I can understand defaulted constructors, since user defined constructors will disable the compiler generated ones making the object non trivially copyable etc.

在析构函数中,访问类别,什么用来定义默认析构函数,考虑没有用户定义的成员函数可以禁用它们(你不能重载析构函数)。

In the destructor case, apart from changing access category, what use is there to define a defaulted destructor considering that no user defined member function can disable them (you can't overload destructors anyway) ?

// Which version should I choose ? 
struct Example 
{
    //1. ~Example() = default; 
    //2. ~Example() {}
    //3. 
}; 

即使在虚拟析构函数的情况下,默认它们不会使它们微不足道,一个用途是使析构函数 protected 或<$

Even in the case of virtual destructors, defaulting them would not make them trivial so what good is it doing it?

推荐答案

另一个:在写类时,一些程序员喜欢命令类的函数:eg构造函数,然后是析构函数,然后是非 - const mutating成员,然后是 const accessor然后 static 函数。通过能够显式地 = default 析构函数,你可以按预期的顺序列出它,并且看到那里的读者知道不能有另一个错位的版本。在大类中,这可能有一些纪录片/安全值。

Another: when writing classes, some programmers like to order the class's functions: e.g. constructors, then the destructor, then the non-const "mutating" members, then the const "accessor" members, then static functions. By being able to explicitly = default the destructor, you can list it in the expected order and the reader looking there knows there can't be another misplaced version of it. In large classes, that may have some documentary/safety value.

它还给你一些具体的围绕其添加注释,这可以帮助一些文档工具实现评论破坏。

It also gives you something concrete around which to add comments, which can help some documentation tools realise the comments relate to destruction.

这篇关于什么是默认析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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