具体类的析构函数

发布于 2024-08-10 22:55:09 字数 288 浏览 7 评论 0原文

指南 #4 链接文本,指出:

基类析构函数应该是 公共和虚拟的,或者 受保护的和非虚拟的。

可能我错过了一些东西,但是如果我只是创建一个具体的类,而不是设计用作基类怎么办?

我应该声明它的析构函数是公共的和虚拟的吗?通过此,我含蓄地声明我的类“已准备好用作基类”,但这不一定是真的。

Guideline #4 link text, states:

A base class destructor should be
either public and virtual, or
protected and nonvirtual.

Probably I'm missing something, but what if I just create a concrete class, that is not designed to be used as base class.

Should I declare it's destructor public and virtual? By this I'm implicitly declate that my class is "ready to be used as base class", while this is not necessary true.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

撑一把青伞 2024-08-17 22:55:09

链接文本特别指出“基类析构函数应该是”...

该指南仅适用于设计用作基类的类。如果您要创建一个不用作基类的具体类,则应将公共构造函数保留为非虚拟构造函数。

The link text specifically says"A base class destructor should be"...

The guidelines are only meant for a class which is designed to be used as a base class. If you are making a single, concrete class that will not be used as a base class, you should leave the public constructor non-virtual.

星軌x 2024-08-17 22:55:09

如果你的类中没有其他东西是虚拟的,我认为析构函数也不应该是虚拟的。

If nothing else in your class is virtual, I don't think the destructor should be virtual either.

倾听心声的旋律 2024-08-17 22:55:09

换句话说:你知道没有人绝对会尝试从你的班级中派生吗?当有人这样做时,你认为他会记得仔细看看你的博士吗?有时人们使用继承而不是组合是有充分理由的(提供类的完整接口,而无需使用丑陋的 getter 语法)。
虚拟 dtor 的另一点是开放/封闭原则
如果您不关心硬实时性能或类似的东西,我会选择虚拟 dtor。

Consider it another way around: Do you know that no one will absolutely ever try to derive from your class and when somebody does do you think he will remember to take a closer look at your dtor? Sometimes people use inheritance over composition for a good reason (provide the full interface of your class without having ugly getter syntax).
Another point for the virtual dtor is the Open/Closed Principle.
I'd go with the virtual dtor if you are not concerned with hard real-time performance or something alike.

木有鱼丸 2024-08-17 22:55:09

在以下任何情况下,析构函数应为虚拟:

  • 您的类包含任何虚拟方法。
  • 即使没有什么是虚拟的,您也计划使用类作为基础。

罕见例外

  • 您试图保存 4 个字节,因此虚拟表指针不是可接受的解决方案(例如 - 由于某种原因,您的类必须适合 32 位)。但要做好迎接地狱的准备。

关于公共或受保护 - 一般来说,更多的问题是您打算如何控制对析构函数的访问。

Destructor SHALL BE virtual in any of the following cases:

  • Your class contains ANY virtual method.
  • Even if nothing is virtual you plan to use class as base.

Rare exception:

  • You are trying to save 4 bytes and virtual table pointer is NOT ACCEPTABLE solution because of this (example - your class HAS to fit in 32 bits because of some reason). But be prepared for hell.

Regarding public or protected - in general it is more question of how you intend to control access to destructor.

影子是时光的心 2024-08-17 22:55:09

仅当您的类稍后要扩展时,您的析构函数才需要是虚拟的。我不知道您需要受保护/私有析构函数的情况。

值得注意的是,如果您有一个虚拟方法,您也不会损失任何东西(对于大多数编译器),使析构函数也成为虚拟的(但它会保护您,以防以后有人扩展)。

Your destructor only needs to be virtual if your class will be extended later. I'm not aware of a case where you'd want a protected/private destructor.

It's worth noting that if you have even one virtual method, you lose nothing (with most compilers) making the destructor virtual as well (but it will protect you in case somebody extends later).

提赋 2024-08-17 22:55:09

该建议指的是具有虚函数的类,旨在成为多态基类。您必须确保如果有人在基类指针上调用delete,则会调用实际类的析构函数;否则,派生类分配的资源将不会被释放。

有两种方法可以实现这一点:

  • 公共虚拟析构函数,以便在运行时找到正确的析构函数;或
  • 受保护的非虚拟析构函数,它可以防止在基类指针上调用delete

对于不用作基类的具体类,您只会在指向实际类型的指针上调用delete,因此该建议不适用。如果需要的话,它应该有一个公共非虚拟析构函数。

The advice refers to classes with virtual functions, intended to be polymorphic base classes. You have to make sure that if someone calls delete on a base class pointer, then the destructor of the actual class is called; otherwise, resources allocated by the derived classes won't be freed.

There are two ways to achieve this:

  • a public virtual destructor, so the correct destructor is found at runtime; or
  • a protected non-virtual destructor, which prevents calling delete on a base class pointer.

For a concrete class that won't be used as a base class, you will only ever call delete on a pointer to the actual type, so the advice doesn't apply. It should have a public non-virtual destructor if it needs one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文