使用对象的一种行为来整体修改对象是否有意义?话说里面的赋值运算符重载用的是this指针吗?
我看到一个关于这个运算符的问题(分配给 *this 会做什么( *this = val)?)
但是从对象的一种行为内部修改整个对象是否合乎逻辑?这在 OOP 世界中有意义吗?
I see a question on this operator (What does assignment to *this do (*this = val)?)
but is it logical to modify an object as a whole from within one of its behavior? Does it makes sense in OOP world?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您链接到的问题中给出的示例只是根据另一个赋值运算符定义了一个赋值运算符,即它允许将 GUID 分配给 QUuid ,并且只需简单地执行此操作即可构造一个
QUuid
并使用QUuid
的“常规”赋值运算符将另一个QUuid
作为参数。所以,是的,这是完全有道理的。人们期望赋值运算符能够改变整个实例。对于其他成员函数,除非它们被适当命名以表明它们正在执行类似于赋值的函数,否则我不会期望看到
*this =
。The example given in the question you linked to is simply defining an assignment operator in terms of another one i.e. it allows one to assign a
GUID
to aQUuid
and does so by simply constructing aQUuid
and the using the "regular" assigment operator forQUuid
that takes anotherQUuid
as an argument.So, yes, it makes perfect sense. One would expect an assignment operator to change the whole instance. For other member functions, unless they are appropriately named to suggest that they are performing a function similar to assignment, I would not expect to see
*this =
.当从设计角度来看有意义时,它才有意义。例如,我希望
Rect
、Size
和Point
类支持赋值。从这个意义上说,这些类型(对我来说)应该表现得像普通旧数据类型(POD)。但如果我用汽车和卡车进行交通模拟建模,我肯定不会有分配运算符。将一辆车设置为与另一辆车相同是没有任何意义的。我可能会
克隆
一辆车(制作一辆看起来与另一辆车一模一样的新车),但是一旦创建了一辆车,它就永远是那辆车。它不能成为另一种汽车。作为进一步的示例,考虑一个
Person
对象:您希望
Person
具有赋值运算符吗?不可以。一个人不能成为另一个人。然而,他的名字又如何呢?当然!一个人可以改变他的名字,所以名字上的赋值运算符是非常有意义的。因此,要回答您的问题,在有意义的地方这样做是有意义的:)
It makes sense when it makes sense from a design perspective. I'd expect
Rect
,Size
andPoint
classes, for example, to support assignment. In this sense, these kinds of types (to me) should behave like plain-old-datatypes (PODs).But if I was modelling a traffic simulation with cars and trucks, I certainly would not have an assignment operator. Setting one car to equal another just doesn't make any sense. I might
clone
a car (Make a new one that looks just like the another one), but once a car is created it will always be that car. It can't become another kind of car.As a further example, consider a
Person
object:Would you expect a
Person
to have an assignment operator? No. A Person cannot become another person. However, what about his name? Sure! A Person can change his name, so an assignment operator on the name makes perfect sense.So to answer your question, it makes sense to do it where it makes sense to do it :)
这完全取决于你的代码的逻辑。通过“作为一个整体”修改对象,您可以确定不会破坏它的不变量。通常,如果类提供了operator=,那么使用它并没有什么问题。
在提到的问题中,调用重载赋值运算符是非常合乎逻辑的方式,所以这是一个很好的例子。
it completely depends on the logic of your code. by modifying object "as a whole" you're sure that you don't brake its invariants. usually if class provide operator= nothing wrong to use it.
in referred questions it's pretty logical way to call overloaded assignment operator, so it's a good example.
我不知道这在 OOP 中是否有意义,但 C++ 不是纯粹的 OOP,它是一种多范式语言,为了正确使用 C++,你必须理解它,而不是 OOP 或其他范式。
C++ 中的成员函数只是带有隐式
this
参数的函数。 (A
类的函数成员有一个A *this
隐式参数。)然后它隐式传递 < code>this 到同一类中的其他函数。然后函数可以根据需要使用
this
,这在 C++ 中是有意义的。I don't know if it makes sense in OOP, but C++ isn't pure OOP, it's a multi paradigm language and in order to correctly use C++ you must understand it, not OOP or other paradigms.
A member function, in C++, is just a function whit an implicit
this
parameter. (A function member of classA
has got anA *this
implicit parameter.) Then it implicitly passesthis
to the other functions in the same class.A function can then use
this
as it wants to, and this makes sense in C++.