订阅所有文章
文章搜索

高级搜索这是社么?这是顶尖最新推出的文章增强型搜索功能!
全网 本站
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> VC >> 文章正文

MFC的 CString 学习笔记 -3

作者:handi  来源:CSDN  点击  更新:2006-12-19 7:07:16  编辑: 画王w  字体

anged at the same time, that may be is not we want.

                There is a function in MFC named CopyBeforeWrite to deal with this problem:

void CString::CopyBeforeWrite()

{

                if (GetData()->nRefs > 1)

                {

                                CStringData* pData = GetData();

                                Release();

                                AllocBuffer(pData->nDataLength);

                                memcpy(m_pchData, pData->data(), (pData->nDataLength+1)*sizeof(TCHAR));

                }

                ASSERT(GetData()->nRefs <= 1);

}

                When one CString’s value will be set, MFC will check if there is any other  CString share the same CStringData object with it. If the reference bigger than 1, that means at least 2 CString object reference this CStringData object. The current, will be changed CString must create a new CStringData object to store the new value.

                In the Release() function, if the reference bigger than 1, the buffer will not delete actually.

 

6. LockBuffer() and UnLockBuffer()

                LockBuffer will set nRef member to -1, and UnlockBuffer will set nRef member to 1.

                MSDN said: While in a locked state, the string is protected in two ways:

No other string can get a reference to the data in the locked string, even if that string is assigned to the locked string.

 

The locked string will never reference another string, even if that other string is copied to the locked string.

               

                CString str1, str2;

                str2 = "abcde";

                str2.LockBuffer();

                str1 = str2;

 

                CString str3;

                str3 = "kk";

                str3.LockBuffer();

                str3 = str1;

                The str2’s buffer is locked, when set str1 equal to str2, a new buffer will created to store the str2’s value. str1 and str2 have different CStringData object.

               If the str3’s buffer is not locked, the str3 will share CStringData object with str1, but in this code, str3’s CStringData object is locked, MFC will copy the value of str1 to str3, the str3’s buffer address is not changed.

                Of course , when CString is initializion without init-value, MFC will set a system defined, empty CStringData object to it, the nRef is -1, so LockBuffer() and UnLockBuffer() is no use to system defined, empty CStringData object.

 

上一页  [1] [2] 

  • 上一篇文章:

  • 下一篇文章:
  •      
    热门文章  
    推荐文章  
    相关文章    
     发表评论
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备05002835号