1

Why is there an L before the string when creating a wstring?

e.g. -

std::string = "regular string :3";
std::wstring = L"wide string :/";

Is it because it's referenced with the LPSTR, LPWSTR, and LPCSTR's?

Figment
  • 105
  • 1
  • 5
  • 1
    I reopened this question because (1) my sense is that the OP is still learning C++ and (2) the question suggested as a duplicate didn't provide an answer that the OP would have understood and found useful. – templatetypedef Jul 29 '16 at 02:28
  • Will be looking for a book, any suggestions? Maybe a book toward writing and getting process in windows? Thanks. – Figment Jul 29 '16 at 03:08

1 Answers1

14

You're on the right track that this has something to do with LPSTR, LPWSTR, and LPCSTR, but that's not precisely what's going on here. Those types aren't actually a part of standard C++ and are a Microsoft-specific set of types that are used with the Windows API.

C++ has two built-in types for characters, char, which is used with std::string, and wchar_t, which is used with std::wstring. If you write a string literal in regular quotes, C++ treats it as an string made of chars, which can be used with std::string. To tell C++ that you're trying to make a string literal made of wchar_ts - which is what you'll need to use to work with std::wstring, you need to prefix it with an L because that's just how C++ is defined.

Note that this is totally independent of the Microsoft types mentioned above.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • And the LPVOID since it says it's a pointer of anytype, using int *pointer would be for C++, and LPVOID would be used from Windows?? – Figment Jul 29 '16 at 03:05
  • Microsoft's LPVOID is analogous to C++'s `void *`. Doing some searches online should point you in the right direction on that one. (Many of these old Microsoft types were from the Bad Old Days of short and long pointers and are usually just nice typedefs around built-in C++ types.) – templatetypedef Jul 29 '16 at 03:08