Re: Why string's c_str()? [Overloading const char *()]
DSF <notavalid@address.here> wrote in
news:2rva79leu30dpkocg578pne9t3h8jvbv56@4ax.com:
Why do I care? Because I use raw pointers all the time. I write
Windows code, and 70% of the API calls involve a pointer to a
character string, pointer to a structure, pointer to a buffer, etc.
That's because Windows API is defined in terms of C and not C++.
In C++ one usually writes wrappers or uses other C++ libraries in order
to encapsulate a C API, so the rest of the code can use normal C++ style.
In the wrapper code you need pointers and buffers indeed, but this is a
localized one-time activity.
Example: encapsulate getcwd():
sdt::string My_getcwd() {
wchar_t buff[MAX_PATH];
DWORD n = ::GetCurrentDirectoryW(MAX_PATH, buff);
if (n==0 || n>MAX_PATH) {
throw MyException("my_getcwd failed: " + MyGetLastErrorString());
}
return Win2UtfFileName(std::wstring(buff, n));
}
Here, Win2UtfFileName() is another wrapper function wrapping
WideCharToMultiByte() on Windows an converting Windows UTF-16 to more
portable UTF-8, but that's not the main point here.
Third-party libraries like boost::filesystem probably do this better. But
in any case you should use C++ interfaces in the bulk of your codebase
and not struggling with C pointer-and-buffer madness all the time.
Cheers
Paavo