天天看點

使用GetBuffer之後一定要ReleaseBuffer?

The GetBuffer and ReleaseBuffer memberfunctions allow you to gain access to the internal character buffer of aCString object and modify it directly.

Call GetBuffer for a CString object andspecify the length of the buffer you require. Use the pointer returned byGetBuffer to write characters directly into the CString object.

If you use the pointer returned byGetBuffer to change the string contents, you must call ReleaseBuffer beforeusing any other CSimpleStringT member methods.

Call ReleaseBuffer for the CString objectto update all the internal CString state information, such as the length of thestring. After modifying a CString object's contents directly, you must call ReleaseBufferbefore calling any other CString member functions.

從MSDN的描述中可以知道,GetBuffer讓我們獲得CString對象字元記憶體的操作權,這樣就可以操作CString對象了。但修改CString對象後,對象的狀态資訊并沒有更新,于是便有了ReleaseBuffer。如果你之後需要繼續使用該CString對象,就需要調用ReleaseBuffer。

c++

繼續閱讀