如何防止类在C ++中可继承 [英] How to prevent a class from being inheritable, in C++

查看:70
本文介绍了如何防止类在C ++中可继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我不知道如何在主题行中表达自己,但是我想知道的是什么

: < b>我有一个A类,所以我需要做什么

以确保没有人可以从中得到它?< / b>


Saumya

Hi,

I didn''t know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?</b>

Saumya

推荐答案

Saumya写道:
Saumya wrote:


我我不知道如何在主题行中表达自己,但是我想知道的是:< b>我有一个A类,所以我该怎么做呢
确保没有人可以从中得到它?< / b>

Saumya
Hi,

I didn''t know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?</b>

Saumya




你为什么要这样做?用这个

行为来解释你想要实现的目标将有助于找到一个有意义的解决方案。


-

来得到我的真正的电子邮件地址,删除两个onkas

-

Dipl.-通知。 Hendrik Belitz

中央电子学院

研究中心Juelich



Why do you want to do this? Explaining what you want to achieve with this
behaviour would help to find a meaningful solution.

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich


http://www.parashift.com/c ++ - faq-lit ... .html#faq-23.8

http://www.parashift.com/c++-faq-lit....html#faq-23.8


正如Christian已经发布的那样,至少有三种方法可以做到。但是:在C ++中没有办法像

final那样简单。在Java中:

第一种方法需要额外的静态成员函数,第二种只有评论的方法只有

如果用户合作则有效,而第三种方法需要朋友 ;虚拟基类中的声明,

也不好。在我看来,最终对你的课程使用施加了很大的限制。


因此请三思而后行,你是否不能改变你的设计需要制作

你的班级表现得像最后一样更多。


干杯,

Philipp。


" Saumya" < SA ********** @ yahoo.com>在留言新闻中写道:e9 ************************** @ posting.google.c om ...
As Christian already posted, there are at least three ways to do so. But: There''s no way in C++ as simple as
"final" in Java:
The first approach needs additional static member functions, the second comment-only approach only
works iff the user cooperates, and the third one needs a "friend" declaration in a virtual base class, which
isn''t nice either. And in my opinion "final"ization imposes a large limitation to the usage of you class.

Therefore think twice, please, whether you can''t change your design such not to need to make
your class behave like "final" any more.

Cheers,
Philipp.

"Saumya" <sa**********@yahoo.com> wrote in message news:e9**************************@posting.google.c om...


我不知道如何在主题行中表达自己,但是我想知道的是:< b>我有一个A类,所以我该怎么做
以确保没有人可以从中得到它?< / b>

Saumya
Hi,

I didn''t know how else to express myself in the subject line, but what
I want to know is this: <b>I have a class A, so what do I have to do
to ensure that no one can derive from it?</b>

Saumya



这篇关于如何防止类在C ++中可继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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