什么时候应该防止隐式破坏?它是如何工作的? [英] When should I prevent implicit destruction? How does it work?

查看:206
本文介绍了什么时候应该防止隐式破坏?它是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以声明一个析构函数 = delete private ,以防止程序隐式删除对象在作用域的末尾。我也知道,如果它是私有的,我可以有一个成员函数,可以显式调用析构函数每当我调用它: void kill(){this->〜A(); }




  • p>为什么我想要防止隐式破坏?请举个例子


  • = delete 会做什么?它确保析构函数永远不会运行吗?因此,对象将永远存在于其范围之外?



解决方案

私有析构函数通常用于防止其他程序员对你的类型执行某些操作。私人破坏者特别阻止以下内容:




  • 在堆栈上声明您的实例类型

  • 通过delete关键字手动删除您的类型的实例

  • 手动析构函数调用



做任何这些都将引发一个编译错误,这不是微不足道的解决。错误通常是从作者到用户的消息,他们不应该执行这些操作中的一个或全部,而是作者可能希望他们:




  • 调用工厂函数以销毁此类型的实例(通常与私有构造函数耦合)。在批量操作比一次性分配更有效率的情况下,提供关于构建和销毁的额外上下文可以提供优化机会或防止API滥用。

  • 不分配此类型的任何实例也许是单身人士有什么不好



考虑编写管理硬件接口的类型,允许用户简单地删除实例,从而使硬件处于不良状态为什么还要允许呢?是的,在某些时候,API可以构建来抽象出这个难题 - 但在某些基本层面,需要暴露脆弱功能。



= delete是新的防止用户错误的防弹方法。与私有副本构造函数不同,它不能通过friend关键字绕过。它还倾向于更好地读取,因为它以一致的方式告诉用户代码该功能不可用。我的理解是=删除是在c ++ 11中引入的,作为no copy / no delete idioms的替代。


I know that I can declare a destructor =delete or private in order to prevent the program from implicitly deleting the object at the end of scope. I also know that if it's private, I can have a member function that can explicitly call the destructor whenever I call it: void kill() { this–>~A(); }

My questions are:

  • Why would I ever want to prevent implicit destruction? Please give an example

  • What would =delete do? Does it make sure the destructor never runs? So the object will exist forever outside its scope?

解决方案

Idioms like a private destructor are generally used to prevent other programmers from performing certain operations with your type. A private destructor in particular prevents the following:

  • Declaration of a type of your instance on the stack
  • Manual deletion of an instance of your type via the delete keyword
  • Manual destructor calls

Doing any of these will raise a compile error that is not trivial to work around. The error is usually a message from the author to a user that they should not perform one or all of these operations, instead the author may want them to:

  • Call factory functions to destroy instances of this type (typically coupled with private constructors). Providing extra context on construction and destruction can provide optimization opportunities or prevent API misuse when a batched operation is much more efficient than a "one off" allocation.
  • Not allocate any instances of this type (maybe it's a singleton)

Consider writing a type that manages a hardware interface, allowing the user to simply delete the instance might leave hardware in an undesirable state - so why even allow it? Yes, at some point the API can be built up to abstract away this difficulty - but at some basic level 'fragile' functionality needs to be exposed.

=delete is the 'new' bullet proof way of preventing user error. Unlike the private copy constructor it cannot be circumvented via the 'friend' keyword. It also tends to read better, in that it tells the user of the code in a consistent fashion that this feature is not available. My understanding is that =delete was introduced in c++11 as a replacement for the "no copy/no delete idioms".

这篇关于什么时候应该防止隐式破坏?它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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