Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将long转换为wchar\t*_C++_Performance_Visual C++ - Fatal编程技术网

C++ 将long转换为wchar\t*

C++ 将long转换为wchar\t*,c++,performance,visual-c++,C++,Performance,Visual C++,谢谢你的阅读 我想我已经接近这一点了,但我可以利用一些专业知识来理解为什么我的尝试没有达到我预期的效果 我现在需要的是将一个long类型(long,因为我实际上使用VC++,但我知道那只是一个typedef)转换成wchar\u t*。我能够获得我想要的结果,但我无法理解为什么我认为应该有效的方法……不起作用。下面是实现我想要的结果的代码: const std::wstring myString{ std::to_wstring(wRect.bottom) }; const wchar_t *

谢谢你的阅读

我想我已经接近这一点了,但我可以利用一些专业知识来理解为什么我的尝试没有达到我预期的效果

我现在需要的是将一个long类型(long,因为我实际上使用VC++,但我知道那只是一个typedef)转换成wchar\u t*。我能够获得我想要的结果,但我无法理解为什么我认为应该有效的方法……不起作用。下面是实现我想要的结果的代码:

const std::wstring myString{ std::to_wstring(wRect.bottom) };
const wchar_t * myCharPointer{ myString.c_str() };
与我想采取的方法相比:

const wchar_t * lWind{ std::to_wstring(wRect.bottom).c_str() };
在VS社区中使用我的调试器,我已经确定myCharPointer指向正确的值(“600”),但lWind被分配给指向空字符串/空字符串的点(或者可能永远不会得到新的分配)

我对这两种方法的理论性能方面很好奇,即使用两行代码和两种不同类型的变量来获得我想要的值,而不是一行代码。显然,有一些后端代码被调用来执行这些方法中的每一种,但我不确定一种方法是否比另一种方法的性能代价更高


我很感激人们对此的任何反馈;我很可能错过了一些明显的东西,我试图实现的总体目标可能会以更好的方式实现,但现在我想满足我的好奇心。谢谢

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() };

你的意思是要创建一个包含数字以10为基数表示的
wchar\t
字符字符串吗?或者什么?我传递lWind到的函数需要一个wchar\u t*参数。具体来说,它是来自DirectWrite库的DrawText()函数。我使用DirectWrite库作为调试覆盖运行,并设置了一个调试覆盖更新函数,该函数接受覆盖中显示的文本的wchar_t*参数。在这个特殊的例子中,我尝试使用DrawText()函数传递数值以显示在覆盖图上。如果您熟悉这个库,或者对优化我正在尝试的内容的方法有建议,我非常乐意接受建议!我想可能是这样的。谢谢你的确认。我试着跟踪这些痕迹,但是……我仍然熟悉C++,并且不能掌握到底发生了什么。