I am currently making a small C++ tool , and I am kind of shocked on the way of How to deal with strings in C++. This is the first time i need to deal with C++ , I have pretty much experience with Managed languages with strong types like Java , C# but I am getting mad with C++ strings.
Are there any best practices to work with strings in C++ ?
A lot of WIN API functions dealing with different type of “strings”
tchar , char* , LPWSTR , LPCSTR … etc.
and converting each type to other is taking a lot of time for me to implement.
Please suggest your way of dealing with strings when converting one type to another.
Maybe there is some library to use ?
Short answer? Use
std::stringorstd::wstringwherever you can is my advice.You can easily obtain a “c style” string (i.e. a pointer to a null terminate char array
char*by.c_str()which you can pass into most WinAPI functions as is.Most of the other types you mention (
tchar,LPWSTR,LPCSTRetc) are typedefs to C style arrays (or pointers to C style arrays, or char types) for supporting unicode over multibyte character sets.