不可复制 [英] NonCopyable

查看:60
本文介绍了不可复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到一些NonCopyable课程,我只是想知道为什么他们没有b
$有虚拟析构函数。这不是有用的限制吗?事实上,例如,在Boost中似乎有一大堆泛型类,

没有虚拟析构函数,但这确实暴露了公共构造函数。 />
这些不是灾难的秘诀吗?


Boost有一个抽象类,它简单地定义了一个虚拟析构函数;如果

他们希望能够通过基类指针删除,那么程序员会在继承列表的末尾标记这个想法吗? b
br />

Having come across a few NonCopyable classes, I just wondered why they don''t
have virtual destructors. Doesn''t this limit there usefulness? In fact,
there seem a whole bunch of generic classes in Boost, for instance, that
don''t have virtual destructors but that do expose public constructors.
Aren''t these a recipe for disaster?

Boost has an ''abstract'' class that simply defines a virtual destructor; is
the idea that the programmer tags this on the end of the inheritence list if
they want to be able to ''delete'' through a base class pointer?

推荐答案

Tim Clacy写道:
Tim Clacy wrote:
遇到一些NonCopyable类,我只是想知道他们为什么不<有虚拟析构函数。这不是有用的限制吗?事实上,例如,在Boost中似乎有很多泛型类,它们没有虚拟析构函数,但确实暴露了公共构造函数。
不是这些灾难的食谱?


不,为什么?如果有人试图通过指向没有虚拟析构函数的基地的指针删除派生的

类,那么只会出现问题。这个

并不总是可能发生,并且没有虚拟析构函数

几乎说不要那样做。

Boost有一个抽象类,它只是定义了一个虚拟析构函数;如果他们希望能够通过基类指针删除,程序员会在继承列表的末尾标记这个吗?
Having come across a few NonCopyable classes, I just wondered why they don''t
have virtual destructors. Doesn''t this limit there usefulness? In fact,
there seem a whole bunch of generic classes in Boost, for instance, that
don''t have virtual destructors but that do expose public constructors.
Aren''t these a recipe for disaster?
No, why? There''s only a problem if someone tries to delete a derived
class through a pointer to a base without a virtual destructor. This
isn''t always likely to happen, and the absence of a virtual destructor
pretty much says "don''t do that."
Boost has an ''abstract'' class that simply defines a virtual destructor; is
the idea that the programmer tags this on the end of the inheritence list if
they want to be able to ''delete'' through a base class pointer?




不知道,但我喜欢你的解释。 :)



No idea, but I like your interpretation. :)


2004年5月18日星期二18:53:32 +0200,Tim Clacy

< no *** ****@nospamphaseone.nospamdk>写道:
On Tue, 18 May 2004 18:53:32 +0200, "Tim Clacy"
<no*******@nospamphaseone.nospamdk> wrote:
遇到一些NonCopyable类,我只是想知道为什么他们没有虚拟析构函数。这不是有用的限制吗?事实上,例如,在Boost中似乎有很多泛型类,它们没有虚拟析构函数,但确实暴露了公共构造函数。
不是这些灾难的食谱?


通常你从NonCopyable类私下继承(比如

boost :: noncopyable),所以不存在问题。还有

也没理由拥有不可复制的*。您是如何设想这场灾难发生的?
?将虚拟析构函数添加到
boost :: noncopyable将是一件非常糟糕的事情 - 很多类没有虚拟函数的
使用noncopyable,这些会

突然都获得了不必要的vtable(以及相关的RTTI等)。


另外,你能给出这些泛型类的另一个例子吗

可能有问题?我怀疑你看到危险的地方

是没有,或者只有一些是通过严重滥用(比如子类化

类,不应该是子类) 。

Boost有一个''抽象''类,它简单地定义了一个虚拟析构函数;如果他们希望能够通过基类指针删除,程序员会在继承列表的末尾标记这个吗?
Having come across a few NonCopyable classes, I just wondered why they don''t
have virtual destructors. Doesn''t this limit there usefulness? In fact,
there seem a whole bunch of generic classes in Boost, for instance, that
don''t have virtual destructors but that do expose public constructors.
Aren''t these a recipe for disaster?
Generally you inherit privately from NonCopyable classes (like
boost::noncopyable), so there is no likelihood of problems. There is
also no reason to ever have a noncopyable*. How were you envisioning
this disaster occurring? Adding a virtual destructor to
boost::noncopyable would be a terrible thing to do - lots of classes
which don''t have virtual functions use noncopyable, and these would
suddenly all gain an unnecessary vtable (and associated RTTI, etc.).

Also, could you give another example of these generic classes that
might have a problem? I suspect that you''re seeing danger where there
is none, or only some through serious misuse (such as subclassing a
class that shouldn''t be subclassed).
Boost has an ''abstract'' class that simply defines a virtual destructor; is
the idea that the programmer tags this on the end of the inheritence list if
they want to be able to ''delete'' through a base class pointer?




这堂课在哪里?我在一些测试标题中发现了一个类,它被称为抽象,但没有其他人,也没有最终用户。


Tom

-

C ++ FAQ: http://www.parashift.com/c++-faq-lite/

C常见问题: http://www.eskimo.com/~scs/C-faq/top.html


Jeff Schwab写道:
Jeff Schwab wrote:
Tim Clacy写道:
Tim Clacy wrote:
遇到一些NonCopyable类,我只是想知道为什么
他们没有虚拟的析构函数。这不是有限吗?有用吗?事实上,在Boost中似乎有一大堆通用类,例如,没有虚拟析构函数,但它们会暴露公共构造函数。难道这不是灾难的秘诀吗?
不,为什么?如果有人试图通过指向没有虚拟析构函数的基地的指针删除派生的
类,则只会出现问题。这个
并不总是可能发生,并且没有虚拟的析构函数
几乎说不要那样做。
Having come across a few NonCopyable classes, I just wondered why
they don''t have virtual destructors. Doesn''t this limit there
usefulness? In fact, there seem a whole bunch of generic classes in
Boost, for instance, that don''t have virtual destructors but that do
expose public constructors. Aren''t these a recipe for disaster?
No, why? There''s only a problem if someone tries to delete a derived
class through a pointer to a base without a virtual destructor. This
isn''t always likely to happen, and the absence of a virtual destructor
pretty much says "don''t do that."



杰夫,


我很佩服你对程序员的信任,但对我来说,这并不总是很可能

发生意味着它肯定会发生,当它确实发生时,你有,最好是
,内存泄漏:-)



Jeff,

I admire you''re faith in programmers but, to me, "This isn''t always likely
to happen" means it definately will happen and when it does happen you have,
at best, a memory leak :-)

Boost有一个''抽象''简单定义虚拟析构函数的类;如果程序员希望能够通过基类指针删除,那么程序员会在继承列表的末尾标记这个吗?
Boost has an ''abstract'' class that simply defines a virtual
destructor; is the idea that the programmer tags this on the end of
the inheritence list if they want to be able to ''delete'' through a
base class pointer?


<不知道,但我喜欢你的解释。 :)



No idea, but I like your interpretation. :)



这篇关于不可复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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