📄 subject_28008.htm
字号:
<p>
序号:28008 发表者:曦 发表日期:2003-01-17 09:45:54
<br>主题:看书看到重载的时候出现问题了!
<br>内容:就是看不懂,一点都看不懂!<BR>#include <iostream.h><BR>#include <iomanip.h><BR><BR>class PhoneNumber<BR>{<BR> friend ostream &operator << (ostream&, const PhoneNumber &);<BR> friend istream &operator >> (istream&, PhoneNumber &);<BR>private:<BR> char areaCode[4];<BR> char exchange[4];<BR> char line[5];<BR>};<BR><BR>ostream &operator << (ostream &output, const PhoneNumber &num)<BR>{<BR> output << "(" << num.areaCode << ")"<BR> << num.exchange << "-" << num.line;<BR> return output;<BR>}<BR><BR>istream &operator >> (istream &input, PhoneNumber &num)<BR>{<BR> input.ignore();<BR> input >> setw(4) >> num.areaCode;<BR> input.ignore(2);<BR> input >> setw(4) >> num.exchange;<BR> input.ignore();<BR> input >> setw(5) >> num.line;<BR> return input;<BR>}<BR><BR>int main()<BR>{<BR> PhoneNumber phone;<BR><BR> cout << "Enter phone number in the form(123) 456-7890:\n";<BR><BR> cin >> phone;<BR><BR> cout << "The phone number entered was: " << phone << endl;<BR> return 0;<BR>}<BR><BR>不明白这个operator是什么意思,起什么作用,还有为什么书上说cin >> phone;即生成函数调用operator >> (cin, phone);这两个好像没办法联系起来啊!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:不败的石头 回复日期:2003-01-17 10:09:30
<br>内容:operator是重载标识符,意思是重载+,=,<<等运算符<BR>简单解释一下,你就把<<当作一个成员函数(虽然长得怪一点)吧<BR>具体如果去翻书的话,只会越说你越不明白
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:曦 回复日期:2003-01-17 10:37:07
<br>内容:重载运算符是不是可以这么认为(愚见):<BR>当对象调用重载运算符时,对象会生成函数调用,即调用起成员函数,例如s.operator!()。<BR>如果是一般调用就是其本来意思!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -