subject_45702.htm
来自「一些关于vc的问答」· HTM 代码 · 共 36 行
HTM
36 行
<p>
序号:45702 发表者:罗兹维尔 发表日期:2003-07-02 23:16:00
<br>主题:重载operator+的疑惑
<br>内容:一半看到书上写的都是带两个参数的<BR>我自己写了一下带一个参数的,也可以阿<BR>不知道带两个好在那里?<BR><BR>MyString& operator+(MyString &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>// There are three variants:<BR>// CString + CString<BR>// and for ? = TCHAR, LPCTSTR<BR>// CString + ?<BR>// ? + CString<BR><BR>void CString::ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data,<BR> int nSrc2Len, LPCTSTR lpszSrc2Data)<BR>{<BR> // -- master concatenation routine<BR> // Concatenate two sources<BR> // -- assume that 'this' is a new CString object<BR><BR> int nNewLen = nSrc1Len + nSrc2Len;<BR> if (nNewLen != 0)<BR> {<BR> AllocBuffer(nNewLen);<BR> memcpy(m_pchData, lpszSrc1Data, nSrc1Len*sizeof(TCHAR));<BR> memcpy(m_pchData+nSrc1Len, lpszSrc2Data, nSrc2Len*sizeof(TCHAR));<BR> }<BR>}<BR><BR>CString AFXAPI operator+(const CString& string1, const CString& string2)<BR>{<BR> CString s;<BR> s.ConcatCopy(string1.GetData()->nDataLength, string1.m_pchData,<BR> string2.GetData()->nDataLength, string2.m_pchData);<BR> return s;<BR>}<BR><BR>CString AFXAPI operator+(const CString& string, LPCTSTR lpsz)<BR>{<BR> ASSERT(lpsz == NULL || AfxIsValidString(lpsz));<BR> CString s;<BR> s.ConcatCopy(string.GetData()->nDataLength, string.m_pchData,<BR> CString::SafeStrlen(lpsz), lpsz);<BR> return s;<BR>}<BR><BR>CString AFXAPI operator+(LPCTSTR lpsz, const CString& string)<BR>{<BR> ASSERT(lpsz == NULL || AfxIsValidString(lpsz));<BR> CString s;<BR> s.ConcatCopy(CString::SafeStrlen(lpsz), lpsz, string.GetData()->nDataLength,<BR> string.m_pchData);<BR> 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 + -
显示快捷键?