CString::GetbufferSetLength 和 CString::GetBuffer 之间的区别

Jab*_*cky 4 c++ mfc

GetbufferSetLength(some_size)我想知道和之间到底有什么区别GetBuffer(some_size)

该文档对我来说不是很清楚,而且对我来说,这两个函数听起来几乎都做了或多或少相同的事情。

这是我的用例:

CStringA foo;
char *p = foo.GetBufferSetLength(somelength);

// fill the buffer p with exactly somelength (non null) chars
FillCharBufferWithStuff(p, somelength);

foo.ReleaseBuffer(somelength);  // or should I rather use 
                                // ReleaseBufferSetLength(somelength) ?

// now foo contains the somelength characters provided by FillCharBufferWithStuff
Run Code Online (Sandbox Code Playgroud)

如果我使用它GetBuffer来代替GetBufferSetLength它,则可以完成完全相同的工作。

如果有人能阐明这一点,我将不胜感激。

S.M*_*.M. 5

如果您逐字发布函数参数,您会看到差异。

GetBufferSetLength(int nLength);
GetBuffer(int nMinBufferLength);
Run Code Online (Sandbox Code Playgroud)

并且在描述中给出了这种差异。

GetBufferSetLength

nLength
字符缓冲区的确切大小(CSimpleStringT以字符为单位)。

GetBuffer

nMinBufferLength
字符缓冲区可以容纳的最小字符数。该值不包括空终止符的空间。

GetBufferSetLength()如有必要,截断或增加缓冲区长度以完全匹配 中指定的长度nLength

GetBuffer()仅在必要时增加缓冲区长度以匹配 中指定的最小长度nMinBufferLength