Convert long to wchar_t *

Thanks for reading.

I think I'm close to this, but I could use some experience in understanding why my attempt at something does not work as I expect.

I need (at the moment) to convert the long type (LONG, since I'm actually in VC ++, but I know it's just typedef) to wchar_t *. I can get the result that I want, but I can’t understand why the approach, which I think should work ... does not work. Here is the code that achieves the result I want:

const std::wstring myString{ std::to_wstring(wRect.bottom) };
const wchar_t * myCharPointer{ myString.c_str() };

Unlike the approach I would like to take:

const wchar_t * lWind{ std::to_wstring(wRect.bottom).c_str() };

Using my debugger in VS Community, I determined that myCharPointer points to the correct value ("600"), but lWind is assigned to a point (or maybe it never gets a new assignment at all?), To an empty string / empty string.

, .. 2 2 , , 1 . , - , , , , .

; , - , , , . !

+4
1

to_wstring wstring . c_str, , .

const wchar_t * lWind{ std::to_wstring(wRect.bottom).c_str() }; // after this line invalid pointer

, , , wchar_t.

std::wstring ws = std::to_wstring(100);

const wchar_t * lWind{ ws.c_str() };
+2

All Articles