我目前正在學習MFC庫,我想知道爲什麼我應該使用GetBuffer成員,它返回指向CString對象緩衝區的指針,以允許讀取和更改該對象中的字符的其他成員函數? 比如爲什麼我應該做的(代碼修改CString對象的第一個字符):爲什麼我應該使用CString的GetBuffer成員而不是SetAt?
CString aString(_T("String")); //new CString object
LPTSTR p = aString.GetBuffer(); //create new pointer to aString buffer
_tcsncpy(p, LPCTSTR(_T("a")), 1); //set first character to 'a'
aString.ReleaseBuffer(); //free allocated memory
相反的:
CString aStr(_T("String")); //new CString object
aStr.SetAt(0, _T('a')); //set character at 0 position to 'a'
我想有一個更合適的應用程序中使用的GetBuffer()成員,但我無法弄清楚它可能是什麼......這個函數需要ReleaseBuffer()來釋放內存,並且當沒有調用ReleaseBuffer()時我可能導致內存泄漏。使用它有什麼好處嗎?