What are the most used string types in C ++ and how to convert between them?

OR How not to kill yourself or someone the next time the C ++ compiler twists your hand to convert between two arbitrary string types, just to kill you?

I have a complicated temporary encoding in C ++, since I use VB6, C #, Ruby for string operations. But now I spent more than 30 minutes trying to write a line containing 2 guides and a line in the debug window ... and it doesn't get any easier And I already met RPC_WSTR , std::wstring and LPCWSTR

Are there simple (or any) rules to know the transitions between them? Or does this happen only after many years of torture?

Basically, I look for the most commonly used string types in both standard APIs and MS-specific / Visual C ++ libraries; What to do next time, I see

 Error 8 error C2664: 'OutputDebugStringW' : cannot convert parameter 1 from 'std::wstring' to 'LPCWSTR' 

Update : I fixed a compilation error ^^^^. I am not looking for a more global answer to solve a specific problem, which I gave as an example.

+4
source share
6 answers

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; // c now contains "hello world" const char* a = "hello "; const char* b = "world"; const char* c = a + b; // error, you can't add two pointers std:string a = "hello worl"; char b = 'd'; std:string c = a + b; // c now contains "hello world" const char* a = "hello worl"; char b = 'd'; const char* c = a + b; // Doesn't cause an error, but won't do what you expect either. the char 'd' is converted to an int, and added to the pointer `a`. You're doing pointer arithmetic rather than string manipulation. 
+9
source
 OutputDebugStringW (myString.c_str ()); 
+1
source

Welcome to C ++; -)

You can simply create a wrapper function that takes std::string . Then, in the function, cross out the c-style line and go to OutputDebugStringW .

0
source

std::wstring and std::string are just aliases for std::basic_string<wchar_t> and std::basic_string<char> .

Both have a .c_str() method that returns a normal C-string pointer ( LPCWSTR , etc.) and a constructor that takes a C-string.

0
source

You might want to check out CStdString . This is a cross-platform standard C ++ CString implementation that can easily be converted to most other string types. It does almost all the headaches associated with the chain, and this is just one header file.

0
source

Source: https://habr.com/ru/post/1285661/


All Articles