cstring的长度

shuirh 2009-03-24 10:17:10
CString str = "hello";
int n = sizeof (str);

cout << "the size of str is :" << n << endl;
n = str.GetLength();
cout << "the length of str is :" << n << endl;

char *p1 = str.GetBuffer(str.GetLength()); //p1和str共享同一段内存

str.ReleaseBuffer();
strcpy(p1, "aa"); //将这个地方换成"aaaa","aaa","aaaaa"看看
cout << "the content of str is " << p1 << endl;
cout << "the content of p1 is " << p1 << endl;
n = sizeof (str);
cout << "the size of str is :" << n << endl;
n = str.GetLength();
cout << "the length of str is :" << n << endl;

为什么str.getlenth()的返回值老是5,不变呢?
还请大家多多指教,不胜感激.
...全文
128 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangyadong 2009-03-24
  • 打赏
  • 举报
回复
ReleaseBuffer的参数,缺省的是-1。-1表示使用当前的00结束符位置来确定新的长度,所以长度没有改变
以下来自msdn
The new length of the string in characters, not counting a null terminator. If the string is null-terminated, the -1 default value sets the CString size to the current length of the string.


逸萌 2009-03-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wangyadong 的回复:]
ReleaseBuffer的参数,缺省的是-1。-1表示使用当前的00结束符位置来确定新的长度,所以长度没有改变
以下来自msdn
The new length of the string in characters, not counting a null terminator. If the string is null-terminated, the -1 default value sets the CString size to the current length of the string.
[/Quote]
正确

str.ReleaseBuffer(); 放在
strcpy(p1, "aa");后就可以

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧