非会员与会员? [英] nonmember vs member?

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

问题描述

从阅读#84( http://www.gotw。 ca / gotw / 084.htm),我确信

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

然后我来了对此:


Bruce Eckel谈到Thinking In C ++中的运算符重载(第2版 -
http://www.codeguru.com/cpp/tic/tic0129.shtml

" 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

推荐答案

" Erik Haugen" <公顷**** @ Xenon.Stanford.EDU>在留言中写道

新闻:Pi ************************************ ** @ Xeno n.Stanford.EDU ...
"Erik Haugen" <ha****@Xenon.Stanford.EDU> wrote in message
news:Pi**************************************@Xeno n.Stanford.EDU...
[...]
Eckel的论证是否超过萨特的论点?
[...]
Does Eckel''s argument ever outweigh Sutter''s?




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

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

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

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

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

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


Dave


---

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

由AVG反查病毒系统( http://www.grisoft.com)

版本: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




" Erik Haugen" ; <公顷**** @ Xenon.Stanford.EDU>在留言中写道

新闻:Pi ************************************ ** @ Xeno n.Stanford.EDU ...

"Erik Haugen" <ha****@Xenon.Stanford.EDU> wrote in message
news:Pi**************************************@Xeno n.Stanford.EDU...
从阅读#84( http://www.gotw.ca/gotw/084.htm) ,我确信
我应该尝试在非常实用的情况下让非朋友成为非成员但是
然后我遇到了这个:

布鲁斯·埃克尔谈到思考在C ++中的操作符重载(第二版 -
http://www.codeguru.com/cpp/tic/tic0129.shtml
一般来说,如果它没有任何区别,它们应该是成员,以强调操作员和它的班级之间的关联。

这似乎是一个有效的观点,但可能没有Sutter给出的那些令人信服的。例如,非成员非友情可能足够通用,可以在课外使用;没有理由不必要地隐藏课堂内的逻辑。

Eckel的论证是否超过了Sutter的?
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?




我认为这是内涵问题。我认为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".




" David B.保持" < DH *** @ codelogicconsulting.com>在消息中写道

news:bn ********** @ news.astound.net ...

"David B. Held" <dh***@codelogicconsulting.com> wrote in message
news:bn**********@news.astound.net...
" Erik Haugen" <公顷**** @ Xenon.Stanford.EDU>在消息中写道
新闻:Pi ************************************** @ Xeno n.Stanford.EDU ...
"Erik Haugen" <ha****@Xenon.Stanford.EDU> wrote in message
news:Pi**************************************@Xeno n.Stanford.EDU...
[...]
Eckel的论证是否胜过Sutter'?
我不喜欢Eckel'的论点。斯科特迈耶斯还讨论了会员与非会员,并总结得更多
或更不像这样:非会员,如果可以,会员何时必须。
[...]
Does Eckel''s argument ever outweigh Sutter''s?
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".




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

这是他说的,或多或少。


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

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

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


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

函数将成为该规则的成员。事实上,在第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.


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

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