数据成员为“受保护”? [英] data members as "protected"?

查看:59
本文介绍了数据成员为“受保护”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么建议不将数据成员定义为受保护?


感谢您的帮助!

解决方案

法案发布:

为什么建议不将数据成员定义为受保护?

感谢您的帮助!




为什么建议晚上不要独自走动?


感谢您的帮助!

-JKop


" Act" < OT *** @ hash.com>在消息中写道

新闻:Q6 ********************* @ bgtnsc04-news.ops.worldnet.att.net ...

为什么建议不将数据成员定义为受保护?




IMO过于笼统的建议有意义的。从

你在哪里得到这个建议?


指定一个成员(数据或功能)为''受保护''

意味着派生类可以直接访问它们。

如果你需要的话,使用''protected''。


-Mike


"法" < OT *** @ hash.com>写道:

为什么建议不将数据成员定义为受保护?




假设是会员数据放在这个特定的班级

,因为有一些必须维护的不变量,这个

类是指定维护它的那个。


显然,鉴于上述情况,如果

它无法控制对数据的访问,则该类无法确保不变量。

如果假设不成立怎么办?在这种情况下,你也可以将b / b
公开。


Why is it suggested to not define data members as "protected"?

Thanks for help!

解决方案

Act posted:

Why is it suggested to not define data members as "protected"?

Thanks for help!



Why is it suggested to not walk around alone at night?

Thanks for help!
-JKop


"Act" <ot***@hash.com> wrote in message
news:Q6*********************@bgtnsc04-news.ops.worldnet.att.net...

Why is it suggested to not define data members as "protected"?



IMO that''s too general an advice to be meaningful. From
where did you get this suggestion?

Specifying a member (data or function) as ''protected''
means that derived classes have direct access to them.
Use ''protected'' if that''s what you need.

-Mike


"Act" <ot***@hash.com> wrote:

Why is it suggested to not define data members as "protected"?



The assumption is that the member data was put in this particular class
because there was some invariant that has to be maintained and this
class is the one designated to maintain it.

Obviously, given the case above, the class can''t ensure the invariant if
it can''t control access to the data.

What if the assumption doesn''t hold? In that case, you might as well
make the data public.


这篇关于数据成员为“受保护”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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