I am sort of new to C++ and I got troubles understanding the usage of the data types fully.
I have these variables to be applied to createwindow parameters and the class with it. That takes an LPCWSTR data type.
LPCWSTR szTitle = L"Hello";
LPCWSTR szWindowClass = L"There";
Therefore I did that, although, I don't understand why I have to include the L before the string (the debugger put it there to be honest). I have also not too often seen strings be defined as the direct types (instead I often see WCHAR,char, etc). If you would make these variables, how would you write them? I don't believe I should be using LPCWSTR. Again sorry, I am fairly new and I can't find exactly what I'm looking for online.
L is a prefix used for wide strings. Each character uses several bytes (depending on the size of wchar_t ). The encoding used is independent from this prefix.
L is the prefix for wide character literals and wide-character string literals which tells the compiler that the char or string is of type wide-char. w is prefixed in operations like scanning (wcin) or printing (wcout) while operating wide-char type.
The L prefix denotes a wide character/string literal; i.e., it is of type wchar_t instead of char. Unicode based programs typically use wide strings, while ANSI/ASCII based programs typically do not.
To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable. You can look into how variables work in Python in the Python variables tutorial. For example, you can assign a character 'a' to a variable single_quote_character .
C++ has several different character types, and the ones at issue here are char
and wchar_t
, with wchar_t
being a wide character of some implementation-defined type. A string literal in C++ is treated like an array of characters, so you can write
const char* rawString = "I'm a regular old string!";
Because char
and wchar_t
aren't necessarily the same type, you can't write
const wchar_t* rawString = "I'm a regular old string!"; // Error!
because there's a type mismatch: you've got an array of char
s on the right-hand side and a pointer of type const wchar_t*
on the left. As a result, C++ lets you define wide string literals by prefixing a string literal with an L
. the resulting string is then an array of elements of type wchar_t
, so this will compile:
const wchar_t* rawString = L"I'm a wide string!"; // Totally fine!
Microsoft's alias LPCWSTR
is essentially a const wchar_t*
, which is why you need the L
prefix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With