具体类的析构函数
指南 #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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
链接文本特别指出“基类析构函数应该是”...
该指南仅适用于设计用作基类的类。如果您要创建一个不用作基类的具体类,则应将公共构造函数保留为非虚拟构造函数。
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.
如果你的类中没有其他东西是虚拟的,我认为析构函数也不应该是虚拟的。
If nothing else in your class is virtual, I don't think the destructor should be virtual either.
换句话说:你知道没有人绝对会尝试从你的班级中派生吗?当有人这样做时,你认为他会记得仔细看看你的博士吗?有时人们使用继承而不是组合是有充分理由的(提供类的完整接口,而无需使用丑陋的 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.
在以下任何情况下,析构函数应为虚拟:
罕见例外:
关于公共或受保护 - 一般来说,更多的问题是您打算如何控制对析构函数的访问。
Destructor SHALL BE virtual in any of the following cases:
Rare exception:
Regarding public or protected - in general it is more question of how you intend to control access to destructor.
仅当您的类稍后要扩展时,您的析构函数才需要是虚拟的。我不知道您需要受保护/私有析构函数的情况。
值得注意的是,如果您有一个虚拟方法,您也不会损失任何东西(对于大多数编译器),使析构函数也成为虚拟的(但它会保护您,以防以后有人扩展)。
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).
该建议指的是具有虚函数的类,旨在成为多态基类。您必须确保如果有人在基类指针上调用
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:
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.