CString类的用法介绍和自己动手写的CString类

28 篇文章 0 订阅

CString is based on the TCHAR data type. If the symbol _UNICODE is defined for your program, TCHAR is defined as type wchar_t, a 16-bit character type; otherwise, it is defined as char, the normal 8-bit character type. Under Unicode, then, CString objects are composed of 16-bit characters. Without Unicode, they are composed of 8-bit char type.

When not using _UNICODE, CString is enabled for multibyte character sets (MBCS, also known as double-byte character sets, DBCS). Note that for MBCS strings, CString still counts, returns, and manipulates strings based on 8-bit characters, and your application must interpret MBCS lead and trail bytes itself.

CString objects also have the following characteristics:

(1)CString objects can grow as a result of concatenation(串联) operations.

(2)CString objects follow “value semantics.” Think of a CString object as an actual string, not as a pointer to a string.

(3)You can freely substitute(替换) CString objects for const char* and LPCTSTR function arguments.

(4)A conversion operator gives direct access to the string’s characters as a read-only array of characters (a C-style string).

Tip   Where possible, allocate CString objects on the frame rather than on the heap. This saves memory and simplifies parameter passing.

CString assists you in conserving memory space by allowing two strings sharing the same value also to share the same buffer space.  However, if you attempt to change the contents of the buffer directly (not using MFC), you can alter both strings unintentionally. CString provides two member functions, CString::LockBuffer and CString::UnlockBuffer, to help you protect your data.  When you call LockBuffer, you create a copy of a string, then set the reference count to -1, which "locks" the buffer. While the buffer is locked, no other string can reference the data in that string, and the locked string will not reference another string. By locking the string in the buffer, you ensure that the string’s exclusive hold on the data will remain intact. When you have finished with the data, call UnlockBuffer to reset the reference count to 1.

详细介绍见MSDN。。。。。 呵呵。

C++自己实现的CString类:







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值