访问器和mutator的真正问题 [英] The real issue with accessors and mutators

查看:58
本文介绍了访问器和mutator的真正问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stroustrup警告不要使用set和get函数的原因是类型的

对象应该以保持一些

不变量的方式设计。课堂上的所有操作都应该保持不变的
。一个例子是std :: vector<> ;.不变量可以是:std :: vector< T>保持由成员函数std :: vector< T> :: size()返回值给出的元素数量。

。这意味着添加或从向量中删除元素必须以这样的方式发生:

返回值size()反映了生成的元素数量。


因此,

用户不能直接操作的值是size()返回的值。为了这个讨论,我们可以假设这个返回值存储在一些内存位置定义

作为向量的成员字段<>正在讨论中。


我发现一般警告不要过度使用get和set成员函数

模糊,可能有害。有很多很多实例使用这些函数是完全合法的,并且可以用这样的方式完成

以保留有意义的不变量。这种一般性禁令经常被广泛接受,就像

禁令所引起的误导一样,介词是我们不应该结束句子的事情

with has。

-

"如果我们的假设是关于任何事情而不是关于某一个或多个

特别是事情,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题。 - Bertrand

Russell

The reason Stroustrup warns against using set and get functions is that an
object of class type should be designed in such a way as to maintain some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be state
as: a std::vector<T> hold a number of elements given by the return value of
the member function std::vector<T>::size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.

Hence, the value that should not be available for direct manipulation by a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location defined
as a member field of the vector<> under discussion.

I find the general warning against using get and set member functions overly
vague, and potentially harmful. There are many, many, instances where
using such functions is perfectly legitimate, and can be done in such a way
as to preserve a meaningful invariant. Too often such general injunctions
become widely accepted in the same way as the misguidance rendered by the
injunction that a preposition is some thing we should not end a sentence
with has.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

推荐答案

Steven T. Hatton写道:
Steven T. Hatton wrote:
Stroustrup警告不要使用set和get函数的原因是


你能分享至少这个警告的章节号吗?

类的对象类型应该设计成保持一定的不变性。课堂上的所有操作都应该保持不变性。一个例子是std :: vector<> ;.不变量可以是:std :: vector< T>保持由成员函数std :: vector< T> :: size()的返回值给出的多个元素。这意味着从向量中添加或删除元素必须以这样的方式发生,即size()的返回值反映了结果的元素数量。

因此,值不应该由
用户直接操作的是size()返回的值。为了这个讨论,我们可以假设这个返回值存储在一些存储位置中,该存储位置被定义为向量<>的成员字段。正在讨论中。

我发现一般警告反对使用get和set成员函数过于模糊,可能有害。


可能?喜欢剪刀?它们具有潜在的危害性。

有许多实例使用这些函数是完全合法的,并且可以通过这种方式完成
以保留有意义的不变量。很多时候这种一般性的禁令被广泛接受,就像
禁令所引起的误导一样,介词是一些我们不应该用句子结束的事情。
The reason Stroustrup warns against using set and get functions is that an
Can you share at least the chapter number where such warning is?
object of class type should be designed in such a way as to maintain some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be state
as: a std::vector<T> hold a number of elements given by the return value of
the member function std::vector<T>::size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.

Hence, the value that should not be available for direct manipulation by a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location defined
as a member field of the vector<> under discussion.

I find the general warning against using get and set member functions overly
vague, and potentially harmful.
Potentially? Like scissors? They are potentially harmful.
There are many, many, instances where
using such functions is perfectly legitimate, and can be done in such a way
as to preserve a meaningful invariant. Too often such general injunctions
become widely accepted in the same way as the misguidance rendered by the
injunction that a preposition is some thing we should not end a sentence
with has.




我觉得你手上有太多时间。


警告是一个警告。你不能反对它,因为有这样的警告并不成立的情况。你不能争辩说反对驾驶太快的警告是过于模糊和潜在有害。因为

可能存在快速驾驶对生存至关重要的情况。如果一本书的作者必须找借口并为他们放在书中的每一段b $ b段提供解释,那么这个星球上就不会留下任何树木了。 />

V



I think you have too much time on your hands.

A warning is a warning. You cannot argue against it because there are
cases where such warning doesn''t hold. You cannot argue that warning
against driving too fast is "overly vague and potentially harmful" because
there can be instances when driving fast is essential to survival. If the
author of a book has to make excuses and provide explanations for every
passage they put in a book, there will be no trees left on this planet.

V


" Steven T. Hatton" <苏****** @ setidava.kushan.aa>在留言中写道

news:ce ******************** @ speakeasy.net ...
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:ce********************@speakeasy.net...
Stroustrup警告不要使用set和get函数的原因是类类型的对象应该以保持一些
不变的方式设计。课堂上的所有操作都应该保持不变性。一个例子是std :: vector<> ;.不变量可以是:std :: vector< T>保持由成员函数std :: vector< T> :: size()的返回值
给出的多个元素。这意味着从向量中添加或删除元素必须以这样的方式发生,即size()的返回值反映了结果的元素数量。


这个结论不符合。一个更准确的结论是

每次改变size()值的操作都必须相应地改变

元素的数量 - 换句话说,一定不能一个操作

更改size()的值而不执行任何操作。

因此,
用户无法直接操作的值是size()返回的值。为了这个讨论,我们可以假设这个返回值存储在某个存储位置中,
定义为向量的成员字段<>正在讨论中。
The reason Stroustrup warns against using set and get functions is that an
object of class type should be designed in such a way as to maintain some
invariant. All operations on the class should be such that they maintain
the invariant. An example is a std::vector<>. The invariant can be state
as: a std::vector<T> hold a number of elements given by the return value
of
the member function std::vector<T>::size(). This means that adding or
removing elements from the vector must happen in such a way that the
returnvalue of size() reflects the resulting number of elements.
This conclusion does not follow. A more accurate conclusion would be that
every operation that changes the value of size() must change the number of
elements correspondingly--in other words, there must not be an operation
that changes the value of size() and does nothing else.
Hence, the value that should not be available for direct manipulation by a
user is the value returned by size(). We can assume for the sake of this
discussion that this return value is stored in some memory location
defined
as a member field of the vector<> under discussion.




但事实上,*是*一个改变size()值的操作 -

it''名为resize。如果是向量并且我们调用v.resize(n),则v.size()将

随后等于n。当然,该调用的副作用是

导致v中元素的数量为* be * n,或者删除元素,或者

制作新元素。 br />



But in fact, there *is* an operation that changes the value of size() --
it''s named resize. If is a vector and we call v.resize(n), v.size() will
subsequently be equal to n. Of course, a side effect of that call is to
cause the number of elements in v to *be* n, either by deleting elements or
fabricating new ones.


Victor Bazarov写道:
Victor Bazarov wrote:
Steven T. Hatton写道:
Steven T. Hatton wrote:
Stroustrup警告不要使用设置和获取功能是
一个
你能分享至少这个警告的章节号吗?
The reason Stroustrup warns against using set and get functions is that
an
Can you share at least the chapter number where such warning is?




我能找到的一个地方现在提到的设置和获取功能是
$ b $b§24.4.2。我不相信这是他提到

问题的唯一地方。如果时间允许,我会尝试找到其他评论。

我觉得你手上有太多时间。

警告是一个警告。你不能反对它,因为有这种警告不成立的情况。你不能争辩说,反对开车太快的警告是过于模糊和可能有害。因为有可能存在快速驾驶对生存至关重要的情况。如果一本书的作者必须找借口并为他们放在书中的每一段文字提供解释,那么这个星球上就没有树木了。

V



The one place I can find mention of set and get functions right now is
§24.4.2. I don''t believe that is the only place where he mentions the
issue. If time permits, I''ll try to find the other comment.
I think you have too much time on your hands.

A warning is a warning. You cannot argue against it because there are
cases where such warning doesn''t hold. You cannot argue that warning
against driving too fast is "overly vague and potentially harmful" because
there can be instances when driving fast is essential to survival. If the
author of a book has to make excuses and provide explanations for every
passage they put in a book, there will be no trees left on this planet.

V




我认为这种事情很重要。我正在努力解决这些问题中我所处理的代码中的一个棘手的

情况。我有一组

的基础向量,它们是一个类的成员,我想修改它作为另一个类中的计算结果的
。将

可变引用返回给成员库的解决方案使得修改代码更加清晰,但会损害我保证线性独立性的能力

class invariant。


我相信这些警告可能有害的原因是因为

人们可以而且经常不加批判地接受它们并判断另一个人's
相应地工作。

-

如果我们的假设是关于任何事情而不是关于某一个或多个

特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题。 - Bertrand

Russell



I think this kind of thing can be important. I''m grappling with a tricky
situation along these lines in code I''m currently working on. I have a set
of basis vectors which are members of one class, and which I want to modify
as a result of calculations in another class. The solution of returning a
mutable reference to the member bases makes the modification code much
cleaner, but compromises my ability to guarantee linear independence as a
class invariant.

The reason I believe such warnings can be potentially harmfull is because
people can and often do accept them uncritically and judge another person''s
work accordingly.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell


这篇关于访问器和mutator的真正问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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