16,472
社区成员
发帖
与我相关
我的任务
分享
TRACE("CStringTest \n");
CString str="";
UCHAR *buf=new UCHAR[1024];
str+=(UCHAR)0;
str+=(UCHAR)0x1;
str+=(UCHAR)0x31;
str+=(UCHAR)0;
str+=(UCHAR)0x99;
memcpy(buf,str.GetBuffer(str.GetLength()),str.GetLength());
for(int i=0;i<str.GetLength();i++)
{
TRACE("0x%02x ",buf[i]);
}
TRACE("\n");
void AppendChar( _In_ XCHAR ch )
{
UINT nOldLength = GetLength();
int nNewLength = nOldLength+1;
PXSTR pszBuffer = GetBuffer( nNewLength );
pszBuffer[nOldLength] = ch;
ReleaseBufferSetLength( nNewLength );
}
void ReleaseBufferSetLength( _In_ int nNewLength )
{
ATLASSERT( nNewLength >= 0 );
SetLength( nNewLength );
}
void SetLength( _In_ int nLength )
{
ATLASSERT( nLength >= 0 );
ATLASSERT( nLength <= GetData()->nAllocLength );
if( nLength < 0 || nLength > GetData()->nAllocLength)
AtlThrow(E_INVALIDARG);
GetData()->nDataLength = nLength;
m_pszData[nLength] = 0;
}
int GetLength() const throw()
{
return( GetData()->nDataLength );
}
可以看出当使用AppendChar的时候是直接修改记录的 字符串长度的。
调用ReleaseBuffer之后才会重新遍历一下得到长度。int GetLength() const throw()
{
return( GetData()->nDataLength );
}
CStringData* GetData() const throw()
{
return( reinterpret_cast< CStringData* >( m_pszData )-1 );
}