subject_45702.htm

来自「一些关于vc的问答」· HTM 代码 · 共 36 行

HTM
36
字号
<p>
序号:45702 发表者:罗兹维尔 发表日期:2003-07-02 23:16:00
<br>主题:重载operator+的疑惑
<br>内容:一半看到书上写的都是带两个参数的<BR>我自己写了一下带一个参数的,也可以阿<BR>不知道带两个好在那里?<BR><BR>MyString&amp; operator+(MyString &amp;str) //我写的
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:擎天柱 回复日期:2003-07-03 08:11:37
<br>内容:参考CString::operator+<BR><BR>// NOTE: "operator+" is done as friend functions for simplicity<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;There are three variants:<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString + CString<BR>// and for ? = TCHAR, LPCTSTR<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString + ?<BR>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;? + CString<BR><BR>void CString::ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data,<BR>&nbsp;&nbsp;&nbsp;&nbsp;int nSrc2Len, LPCTSTR lpszSrc2Data)<BR>{<BR>&nbsp;&nbsp;// -- master concatenation routine<BR>&nbsp;&nbsp;// Concatenate two sources<BR>&nbsp;&nbsp;// -- assume that 'this' is a new CString object<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;int nNewLen = nSrc1Len + nSrc2Len;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if (nNewLen != 0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AllocBuffer(nNewLen);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(m_pchData, lpszSrc1Data, nSrc1Len*sizeof(TCHAR));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(m_pchData+nSrc1Len, lpszSrc2Data, nSrc2Len*sizeof(TCHAR));<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR><BR>CString AFXAPI operator+(const CString&amp; string1, const CString&amp; string2)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;CString s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;s.ConcatCopy(string1.GetData()-&gt;nDataLength, string1.m_pchData,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string2.GetData()-&gt;nDataLength, string2.m_pchData);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return s;<BR>}<BR><BR>CString AFXAPI operator+(const CString&amp; string, LPCTSTR lpsz)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;ASSERT(lpsz == NULL || AfxIsValidString(lpsz));<BR>&nbsp;&nbsp;&nbsp;&nbsp;CString s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;s.ConcatCopy(string.GetData()-&gt;nDataLength, string.m_pchData,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString::SafeStrlen(lpsz), lpsz);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return s;<BR>}<BR><BR>CString AFXAPI operator+(LPCTSTR lpsz, const CString&amp; string)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;ASSERT(lpsz == NULL || AfxIsValidString(lpsz));<BR>&nbsp;&nbsp;&nbsp;&nbsp;CString s;<BR>&nbsp;&nbsp;&nbsp;&nbsp;s.ConcatCopy(CString::SafeStrlen(lpsz), lpsz, string.GetData()-&gt;nDataLength,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;string.m_pchData);<BR>&nbsp;&nbsp;&nbsp;&nbsp;return s;<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-07-03 23:08:16
<br>内容:我看过CString里面的operate +<BR>不过还不懂为什么要两个参数,一个参数不也行吗<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-07-04 21:22:51
<br>内容:根据c的语法<BR><BR>int i = 1;<BR>int j = 2;<BR>i + j;<BR><BR>//i == 1;<BR><BR><BR>如果重载+后<BR>MyString str1 = "hello ";<BR>MyString str2 = "world";<BR><BR>// str1 + str2 == "hello world"<BR><BR>有伪c语法<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>
<font color=red>答案被接受</font><br>回复者:上海到深圳 回复日期:2003-07-05 21:23:46
<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>
回复者:Let's do better! 回复日期:2003-07-05 21:53:03
<br>内容:其实,两个参数的重载operator+函数是全局函数(类的友员函数),其两个参数是同一类的两个具体对象。形如add(a,b),结果在a中。<BR>而一个参数的重载operator+函数是类的成员函数,其一个参数是该类的一具体对象。形如a.add(b)),结果在a中。
<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 + =
减小字号Ctrl + -
显示快捷键?