wstringstream 到 LPWSTR
我已经使用 wstringstream
构建了一个字符串,并且需要将其分配给 LPWSTR
类型的 struct
成员。我尝试使用 my_stringstream.str().c_str()
但出现以下编译时错误:
无法从“const wchar_t *”转换为“LPWSTR”
我该怎么做?当我尝试在 GUI 中显示字符串时,我尝试了许多不同的转换组合,其中有更多编译时错误或随机术语。
I have built up a string using wstringstream
and need to assign it to a member of a struct
of type LPWSTR
. I try to use my_stringstream.str().c_str()
but get the following compile time error:
cannot convert from 'const wchar_t *' to 'LPWSTR'
How can I do this? I have tried many different combinations of casts with either more compile time errors or random jargon when I try and display the string in a GUI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的函数需要一个指向可修改数据的指针,即
wchar_t*
,但标准字符串类仅公开一个指向常量的指针。假设您的函数实际上可能会写入内存,我们需要为其提供一个有效的指针。与往常一样,获取可修改缓冲区的一个简单方法是使用
向量
:Your function expects a pointer to modifiable data, i.e.
wchar_t*
, but the standard string class only exposes a pointer to constant. Assuming that your function may actually write to the memory, we need to provide it with a valid pointer.An easy way to obtain a modifiable buffer is, as always, a
vector
:LPWSTR
的typedef
为wchar_t*
。您正在尝试将const wchar_t*
转换为wchar_t*
。你不能隐式地这样做。您可以使用
const_cast
来解决这个问题,但前提是您确定该函数不会修改内存:请注意,您不想这样做
const_cast< ;LPWSTR>(my_stringstream.str().c_str())
(除非您将其传递给函数),因为这将创建一个临时字符串对象,获取它的指针,将其转换为LPWSTR
然后从str()
获得的临时字符串将在该行末尾被销毁,使您的LPWSTR
指向已释放的内存块。如果您将
LPWSTR
传递给的函数正在修改字符串,请参阅Kerrek 的回答。LPWSTR
istypedef
d aswchar_t*
. You're trying to convert aconst wchar_t*
to awchar_t*
. You can't do that implicitly.You can get around this by using
const_cast
, but only if you are certain the function won't modify the memory:Note that you do not want to do
const_cast<LPWSTR>(my_stringstream.str().c_str())
(unless you are passing that to a function) because that will create a temporary string object, get it's pointer, convert it to aLPWSTR
and then the temporary string you get fromstr()
will be destroyed at the end of that line, leaving yourLPWSTR
pointing to a deallocated block of memory.If the function you are passing the
LPWSTR
to is modifying the string, see Kerrek's answer.如果您绝对确定字符串的内容不会被修改,则可以通过
static_cast
将const
强制转换;例如,如果您使用某些struct
向函数提供数据,但相同的struct
也用于检索它,则这是可以接受的情况成员是LPWSTR
而不仅仅是LPCWSTR
。相反,如果您要向其传递 struct 的函数需要修改字符串,则有两种选择。
最安全的方法是分配字符串的单独副本作为
WCHAR
的原始动态数组,并将wstring
的内容复制到其中。您可能希望用智能指针包装new
的结果,除非您要转移字符串的所有权(在这种情况下,您可能必须使用一些特殊的分配函数) 。您还可以使用
&YourString[0]
将指针传递给字符串的内部缓冲区,但是 (1) 我不确定它是否能按标准工作,(2) 它仅当该函数不会更改字符串的长度(在字符串末尾添加L'\0'
)时才可以正常工作;在这种情况下,您还应该重新调整字符串的实际长度。在最后一种情况和第一种情况下,您都必须确保您传递
struct
的函数不会期望指向的缓冲区的生存时间比您的wstring
的范围长。 code> (小心:mystream.str()
是一个临时变量,它在您使用它的那一行就消失了,您必须将它分配给一个新变量以赋予它更广泛的范围)。If you are absolutely sure that the content of the string will not be modified, you can cast the
const
away via astatic_cast
; a situation where this can be acceptable for example is if you are using somestruct
to provide data to a function, but the samestruct
is also used for retrieving it, so that member isLPWSTR
instead of just anLPCWSTR
.If, on the contrary, the function to which you'll pass the
struct
needs to modify the string, you have two alternatives.The safest one is to allocate a separate copy of the string as a raw dynamic array of
WCHAR
and copy there the content of thewstring
. You will probably want to wrap the result of thenew
with a smart pointer, unless you're transferring the ownership of the string (and in that case you'll probably have to use some special allocation function).You can also pass a pointer to the internal buffer of the string using
&YourString[0]
, but (1) I'm not sure it's guaranteed to work by the standard, and (2) it works fine only if the function won't change the length of your string adding aL'\0'
before the end of it; in this case you should also re-adjust the actual length of the string.Both in the last and in the first case you must be sure that the function you're passing the
struct
to do not expect the pointed buffer to live longer than the scope of yourwstring
(careful:mystream.str()
is a temporary that dies on the very line you use it, you have to assign it to a new variable to give it a broader scope).其中已知字符串长度小于 101。
它可以在没有
unsetf(std::ios_base::skipws)
和所有这些的情况下工作。并且 wchar_t 数组上没有ZeroMemory
。where string length known to be less then 101.
it works without
unsetf(std::ios_base::skipws)
and all this. And withoutZeroMemory
on wchar_t array.原因很简单:(my_stringstream.str()。 c_str())。不过,我建议反对这一点(因为你可能只是把它搞砸和/或以这种方式修改不同的东西。只有当你确定它不会被修改或者修改无关紧要时才这样做。
LPWSTR
扩展为wchar_t *
。由于指向流内容的指针是 const,因此无法将其转换为 const,除非使用 const_cast最简单的(和最安全的解决方案)是在缓冲区中创建您自己的由
wstringstream
提供的字符串副本,并在结构中引用该字符串,只是不要忘记稍后释放内存。The reason for this is rather simple:
LPWSTR
expands towchar_t *
. As the pointer to the stream contents is aconst
, it's not possible to cast thisconst
away, unless usingconst_cast<LPWSTR>(my_stringstream.str().c_str())
. However I'd advice against this (as you might simply screw this up and/or modify something different that way. Only do it if you're sure it won't be modified or the modification won't matter.The easiest (and most secure solution) is to create your own copy of the string provided by the
wstringstream
in a buffer and refer to this one in the struct. Just don't forget to free the memory later on.