There are two ways to convert a CString
to an LPCWSTR
pointer. Usually, I use the static_cast
method. But recently I have realized GetBuffer()
can be used with the argument 0
. So I think GetBuffer()
is good for more clean code, because static_cast
has an ugly syntax though it looks more C++
.
What are the pros and cons of each way, and what should I use in C++?
#include <atlstr.h>
int main() {
CString aCString = CString(_T("A string"));
_tprintf(_T("%s"), aCString.GetBuffer(0)); //methond -1
_tprintf(_T("%s"), static_cast<LPCWSTR>(aCString)); //methond -2
}
There is no essential difference between the 2 forms. When running, the GetBuffer() method should return some internal buffer, while the static_cast should invoke the convert operator like below.
operator char* () {return _internalBuffer;}
it's personal preference which one you like to use.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.