📄 effective c++ 2e item19.htm
字号:
<P>result = oneHalf.operator*(2); // 运行良好</P>
<P>result = 2.operator*(oneHalf); // 出错!</P>
<P>对象oneHalf是一个包含operator*函数的类的实例,所以编译器调用了那个函数。而整数2没有相应的类,所以没有operator*成员函数。编译器还会去搜索一个可以象下面这样调用的非成员的operator*函数(即,在某个可见的名字空间里的operator*函数或全局的operator*函数):</P>
<P>result = operator*(2, oneHalf); // 错误!</P>
<P>但没有这样一个参数为int和Rational的非成员operator*函数,所以搜索失败。</P>
<P>再看看那个成功的调用。它的第二参数是整数2,然而Rational::operator*期望的参数却是Rational对象。怎么回事?为什么2在一个地方可以工作而另一个地方不行?</P>
<P>秘密在于隐式类型转换。编译器知道传的值是int而函数需要的是Rational,但它也同时知道调用Rational的构造函数将int转换成一个合适的Rational,所以才有上面成功的调用(见条款M19)。换句话说,编译器处理这个调用时的情形类似下面这样:</P>
<P>const Rational temp(2); //
从2产生一个临时<BR>
// Rational对象</P>
<P>result = oneHalf * temp; //
同oneHalf.operator*(temp);</P>
<P>当然,只有所涉及的构造函数没有声明为explicit的情况下才会这样,因为explicit构造函数不能用于隐式转换,这正是explicit的含义。如果Rational象下面这样定义:</P>
<P>class Rational {<BR>public:<BR> explicit Rational(int numerator =
0, //
此构造函数为<BR>
int denominator = 1); // explicit<BR> ...</P>
<P> const Rational operator*(const Rational& rhs) const;</P>
<P> ...</P>
<P>};</P>
<P>那么,下面的语句都不能通过编译:</P>
<P>result = oneHalf *
2; //
错误!<BR>result = 2 *
oneHalf;
// 错误!</P>
<P>这不会为混合运算提供支持,但至少两条语句的行为一致了。</P>
<P>然而,我们刚才研究的这个类是要设计成可以允许固定类型到Rational的隐式转换的——这就是为什么Rational的构造函数没有声明为explicit的原因。这样,编译器将执行必要的隐式转换使上面result的第一个赋值语句通过编译。实际上,如果需要的话,编译器会对每个函数的每个参数执行这种隐式类型转换。但它只对函数参数表中列出的参数进行转换,决不会对成员函数所在的对象(即,成员函数中的*this指针所对应的对象)进行转换。这就是为什么这个语句可以工作:</P>
<P>result = oneHalf.operator*(2); // converts int
-> Rational</P>
<P>而这个语句不行:</P>
<P>result = 2.operator*(oneHalf); //
不会转换<BR>
// int -> Rational</P>
<P>第一种情形操作的是列在函数声明中的一个参数,而第二种情形不是。</P>
<P>尽管如此,你可能还是想支持混合型的算术操作,而实现的方法现在应该清楚了:使operator*成为一个非成员函数,从而允许编译器对所有的参数执行隐式类型转换:</P>
<P>class Rational {</P>
<P>
...
// contains no operator*</P>
<P>};</P>
<P>// 在全局或某一名字空间声明,<BR>// 参见条款M20了解为什么要这么做<BR>const Rational operator*(const
Rational&
lhs,<BR>
const Rational& rhs)<BR>{<BR> return Rational(lhs.numerator() *
rhs.numerator(),<BR>
lhs.denominator() * rhs.denominator());<BR>}</P>
<P>Rational oneFourth(1, 4);<BR>Rational result;</P>
<P>result = oneFourth *
2; // 工作良好<BR>result
= 2 * oneFourth; //
万岁, 它也工作了!</P>
<P>这当然是一个完美的结局,但还有一个担心:operator*应该成为Rational类的友元吗?</P>
<P>这种情况下,答案是不必要。因为operator*可以完全通过类的公有(public)接口来实现。上面的代码就是这么做的。只要能避免使用友元函数就要避免,因为,和现实生活中差不多,友元(朋友)带来的麻烦往往比它(他/她)对你的帮助多。</P>
<P>然而,很多情况下,不是成员的函数从概念上说也可能是类接口的一部分,它们需要访问类的非公有成员的情况也不少。</P>
<P>让我们回头再来看看本书那个主要的例子,String类。如果想重载operator>>和operator<<来读写String对象,你会很快发现它们不能是成员函数。如果是成员函数的话,调用它们时就必须把String对象放在它们的左边:</P>
<P>// 一个不正确地将operator>>和<BR>// operator<<作为成员函数的类<BR>class String
{<BR>public:<BR> String(const char *value);</P>
<P> ...</P>
<P> istream& operator>>(istream& input);<BR>
ostream& operator<<(ostream& output);</P>
<P>private:<BR> char *data;<BR>};</P>
<P>String s;</P>
<P>s >>
cin;
// 合法,
但<BR>
// 有违常规</P>
<P>s <<
cout;
// 同上</P>
<P>这会把别人弄糊涂。所以这些函数不能是成员函数。注意这种情况和前面的不同。这里的目标是自然的调用语法,前面关心的是隐式类型转换。</P>
<P>所以,如果来设计这些函数,就象这样:</P>
<P>istream& operator>>(istream& input, String&
string)<BR>{<BR> delete [] string.data;</P>
<P> read from input into some memory, and make string.data<BR> point
to it</P>
<P> return input;<BR>}</P>
<P>ostream& operator<<(ostream&
output,<BR>
const String& string)<BR>{<BR> return output <<
string.data;<BR>}</P>
<P>注意上面两个函数都要访问String类的data成员,而这个成员是私有(private)的。但我们已经知道,这个函数一定要是非成员函数。这样,就别无选择了:需要访问非公有成员的非成员函数只能是类的友元函数。</P>
<P>本条款得出的结论如下。假设f是想正确声明的函数,C是和它相关的类:</P>
<P>·虚函数必须是成员函数。如果f必须是虚函数,就让它成为C的成员函数。</P>
<P>·operator>>和operator<<决不能是成员函数。如果f是operator>>或operator<<,让f成为非成员函数。如果f还需要访问C的非公有成员,让f成为C的友元函数。</P>
<P>·只有非成员函数对最左边的参数进行类型转换。如果f需要对最左边的参数进行类型转换,让f成为非成员函数。如果f还需要访问C的非公有成员,让f成为C的友元函数。</P>
<P>·其它情况下都声明为成员函数。如果以上情况都不是,让f成为C的成员函数。</P><BR><BR></DIV></DIV></DIV><BR><BR>
<SCRIPT src="Effective C++ 2e Item19.files/get_readnum.htm"></SCRIPT>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#666666
border=0>
<TBODY>
<TR>
<TH id=white bgColor=#990000><FONT
color=#ffffff>对该文的评论</FONT></TH></TR></TBODY></TABLE><BR>
<SCRIPT language=javascript>
<!--
function isEmpty(s)
{
return ((s == null) || (s.length == 0))
}
function submit1()
{
if (isEmpty(document.add_critique.csdnname.value) || isEmpty(document.add_critique.csdnpassword.value) || isEmpty(document.add_critique.critique_content.value))
{
alert('登陆名,密码,评论不能为空!!!!') ;
return false;
}
document.add_critique.submit();
}
//-->
</SCRIPT>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#666666
border=0>
<TBODY>
<TR>
<TH id=white bgColor=#990000><FONT
color=#ffffff>发表评论</FONT></TH></TR></TBODY></TABLE>
<TABLE cellSpacing=1 cellPadding=2 width=770 align=center bgColor=#ffffff
border=0>
<TBODY>
<TR>
<TD>
<FORM name=add_critique action=/develop/add_critique.asp
method=post><INPUT type=hidden value=add name=critique_add> <INPUT
type=hidden value=8787 name=from> 评论人: <INPUT name=csdnname>
密码: <INPUT type=password name=csdnpassword>
评论:<BR> <TEXTAREA name=critique_content rows=8 cols=100></TEXTAREA><BR>
<INPUT onclick=javascript:submit1(); type=button value=发表评论 name=ubmit>
<INPUT type=hidden value=8787 name=id> </FORM></TD></TR></TBODY></TABLE>
<TABLE width=770 border=0>
<TBODY>
<TR>
<TD>
<TABLE cellPadding=2 width=770 border=0>
<TBODY>
<TR>
<TD height=10></TD></TR>
<TR>
<TD align=left width=130><A
href="http://www.csdn.net/news/looknews.asp?id=2313"
target=_blank><IMG src="http://www.csdn.net/job/images/csdn_job.gif"
border=0></A> </TD>
<TD align=middle width=510>
<OBJECT id=Movie1
codeBase=http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0
height=60 width=468
classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000><PARAM NAME="movie" VALUE="http://www.csdn.net/images/ad/csdn_media.swf"><PARAM NAME="quality" VALUE="high">
<EMBED src="http://www.csdn.net/images/ad/csdn_media.swf"
quality=high WIDTH=468 HEIGHT=60
TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</EMBED> </OBJECT></TD>
<TD align=middle width=130><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
src="http://www.csdn.net/images/biaoshi.gif" border=0></A>
</TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD>
<TABLE height=20 cellSpacing=0 cellPadding=0 width=770 align=default
bgColor=#1f60a0 border=0>
<TBODY>
<TR class=font13px vAlign=center align=middle>
<TD class=font13px vAlign=center width=90 height=2></TD>
<TD width=2 rowSpan=2><IMG height=13
src="http://www.csdn.net/images/ad4.gif" width=2></TD>
<TD class=font13px width=90 height=2></TD>
<TD width=3 rowSpan=2><FONT color=#ffffff><IMG height=13
src="http://www.csdn.net/images/ad4.gif" width=2></FONT></TD>
<TD class=font13px width=90 height=2></TD>
<TD width=2 rowSpan=2><IMG height=13
src="http://www.csdn.net/images/ad4.gif" width=2></TD>
<TD class=font13px width=90 height=2></TD>
<TD width=3 rowSpan=2><FONT color=#ffffff><IMG height=13
src="http://www.csdn.net/images/ad4.gif" width=2></FONT></TD>
<TD width=350 height=2><FONT color=#ffffff></FONT></TD>
<TD width=10 rowSpan=2><FONT color=#ffffff><IMG height=11
src="http://www.csdn.net/images/ad3.gif" width=6></FONT></TD>
<TD class=font13px width=60 height=2></TD></TR>
<TR class=font14px vAlign=center align=middle>
<TD class=font13px vAlign=center width=90><A
href="http://www.csdn.net/intro/intro.shtm"><FONT
color=#ffffff>美达美简介</FONT></A></TD>
<TD class=font13px width=90><A
href="http://www.csdn.net/intro/ad.shtm"><FONT
color=#ffffff>广告服务</FONT></A></TD>
<TD class=font13px width=90><A
href="http://www.csdn.net/English/"><FONT
color=#ffffff>英语步步高</FONT></A></TD>
<TD class=font13px width=90><A href="http://www.csdn.net/dev/"><FONT
color=#ffffff>程序员大本营</FONT></A></TD>
<TD align=right width=350><SPAN class=font13px><A
href="mailto:webmaster@csdn.net"><FONT
color=#ffffff>百联美达美科技有限公司</FONT></A></SPAN><FONT color=#ffffff><SPAN
class=font13px> </SPAN></FONT></TD>
<TD class=font13px width=60><FONT color=#ffffff>版权所有</FONT>
<SCRIPT>document.write("<img src=http://202.106.156.10/stat.asp?user=designol&refer="+escape(document.referrer)+"&cur="+escape(document.URL)+" width=0 height=0 border=0>");</SCRIPT>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -