本文介绍了非会员与会员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从阅读#84(,我确信

我应该尝试在不实际的情况下让非功能非成员的功能,但

然后我来了对此:


Bruce Eckel谈到Thinking In C ++中的运算符重载(第2版 -


" In一般来说,如果没有任何区别,他们应该是会员,

强调运营商与其班级之间的联系。


这似乎是一个有效的观点,但可能不像萨特给出的那些令人信服的那样。例如,非成员非友情可能足够通用

可以在课外使用;没有理由在课堂上不必要地隐藏

逻辑。


Eckel的论证是否超过了Sutter的?

-Erik

From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I''m convinced that
I should try to make functions nonfriend nonmembers when practical, but
then I came across this:

Bruce Eckel says about operator overloads in Thinking In C++ (2nd Ed -
http://www.codeguru.com/cpp/tic/tic0129.shtml)
"In general, if it doesn''t make any difference, they should be members, to
emphasize the association between the operator and its class."

That seems like a valid point, but probably not as convincing as the ones
Sutter gives. For example, a nonmember nonfriend might be generic enough
to be usable outside of the class; no reason to unnecessarily hide the
logic inside the class.

Does Eckel''s argument ever outweigh Sutter''s?
-Erik

推荐答案




我根本不喜欢Eckel的论点。斯科特迈耶斯还会讨论成员与非成员之间的关系,并将其总结得更多

或更少这样:非会员何时可以,会员何时

必须。例如,任务操作员必须是b
成员。比较运营商不是。使用免费的

函数,您将获得隐式转换。使用会员

函数,*这永远不会得到隐式转换。

有更多的参数,但现在已经足够了。


Dave


---

外发邮件已通过无病毒验证。

由AVG反查病毒系统(。

版本:6.0.521 /病毒库:319 - 发布日期:2003年9月23日



I don''t like Eckel''s argument at all. Scott Meyers also
talks about member vs. non-member, and sums it up more
or less like this: "non-member when it can, member when
it must". For instance, assignment operators have to be
members. Comparison operators don''t. With free
functions, you get implicit conversions. With member
functions, *this will never get an implicit conversion.
There''s more arguments, but that''s enough for now.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003





我认为这是内涵问题。我认为Eckel的建议是好的:如果

它没有任何区别......。这看起来很模糊,以至于它的价格非常安全:-)另一方面,我认为GotW的建议并不是那么好:

尽可能,更喜欢把功能写成非会员非朋友。

对我来说似乎太过分了。如果您阅读了Eckel的书,您会看到

OO编程的一个要点是减少功能的数量

你必须打电话带有一堆参数,当封装减少时需要参数。 GotW建议似乎与

泛型编程一致。 (类似于一些模板和STL的原则

的东西。)但毕竟,几乎不可能*所有*都是非会员非朋友

功能,如果发送它足够的参数?我不喜欢他们的短语

尽可能。



I think it''s a matter of connotations. I think Eckel''s advice is good: "If
it doesn''t make any difference....". That seems vague enough that it''s
pretty safe :-) On the other hand, I think the GotW advice is not so good:
"Where possible, prefer writing functions as nonmember nonfriends." That
seems too far reaching for me. If you read Eckel''s book, you''ll see that
one of the points of OO programming is to reduce the number of functions
that you have to call with a bunch of parameters, when encapsulation reduces
the need for parameters. The GotW advice seems to be consistent with
"generic programming" (similar principles to some of the template and STL
stuff.) But after all, can''t almost *everything* be a nonmember nonfriend
function, if you send it enough parameters? I don''t like their phrase
"where possible".





或多或少?我不同意。我认为你是脱离背景的。

这是他说的,或多或少。


如果功能是虚拟的,当然它必须是会员。如果函数

是<<或>>运营商,使其成为非会员。如果函数需要在其最左边的参数上键入

转换,请将其设为非成员(请注意这是运算符中常见的
)。其他一切都应该是一个会员功能。


这很难非成员,当它可以时。事实上,绝大多数

函数将成为该规则的成员。事实上,在第19项Effective

C ++中,operator *和operator<<和运算符>>是* * *
用于表达他的观点的唯一*函数。

例如,赋值运算符必须是
成员。比较运算符不是。



"more or less"? I don''t agree. I think you''re taking it out of context.
Here is what he says, more or less.

If the function is virtual, of course it must be a member. If the function
is the << or >> operator, make it non-member. If the function needs type
conversion on its left most argument, make it non-member (note this is
common in operators). Everything else should be a member function.

That''s hardly "non-member when it can". In fact, the vast majority of
functions will be member by that rule. In fact, in Item 19 of "Effective
C++", operator* and operator<< and operator>> are the *only* functions he
uses at all to make his point.
For instance, assignment operators have to be
members. Comparison operators don''t.




您使用运算符作为示例。编写的绝大多数OO函数

都是非运算符。 OP没有特别询问运营商

,只是函数。



You''re using operators as an example. The vast majority of OO functions
written are non-operator. The OP didn''t ask anything about operators
specifically, just functions.


这篇关于非会员与会员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:08