There are two built-in string types:
- C ++ strings use the class std :: string (std :: wstring for wide characters)
- C-style strings are const char pointers const char *) (or
const wchar_t* )
Both can be used in C ++ code. Most APIs, including Windows, are written in C, so they use char pointers, not the std :: string class.
Microsoft also hides these pointers behind several macros.
LPCWSTR is a long pointer to the string string Const, or, in other words, const wchar_t* .
LPSTR is a long pointer to a string, or, in other words, a char* (not const).
They have a few more, but they are pretty easy to guess as soon as you recognize these first few. They also have * TSTR options, where T is used to indicate that it can be either regular or wide characters, depending on whether UNICODE is included in the project. LPCTSTR enables LPCWSTR if UNICODE is specified, and LPCSTR otherwise.
So, when working with strings, you just need to know the two types that I have listed above. The rest are just macros for various versions of the char pointer version.
Converting from a char pointer to a string is simple:
const char* cstr = "hello world"; std::string cppstr = cstr;
And the other way is not so much:
std::string cppstr("hello world"); const char* cstr = cppstr.c_str();
That is, std::string takes a C-style string as an argument in the constructor. And it has a member function c_str() that returns a C style string.
Some commonly used libraries define their own string types, and in these cases you will need to check the documentation for how they interact with the "correct" string classes.
Usually you prefer the C ++ std::string , because, unlike char pointers, they behave like strings. For instance:
std:string a = "hello "; std:string b = "world"; std:string c = a + b;