C++ 将 void* 转换为 std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3076968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a void* to a std::string
提问by Lewis
After perusing the web and messing around myself, I can't seem to convert a void*'s target (which is a string) to a std::string. I've tried using sprintf(buffer, "%p", *((int *)point));as recommended by this pageto get to a C string, but to no avail. And sadly, yes, I have to use a void*, as that's what SDL uses in their USEREVENT struct.
在仔细浏览网络并弄乱自己之后,我似乎无法将 void* 的目标(它是一个字符串)转换为 std::string。我已经尝试sprintf(buffer, "%p", *((int *)point));按照本页的建议使用来获取 C 字符串,但无济于事。遗憾的是,是的,我必须使用 void*,因为这就是 SDL 在其 USEREVENT 结构中使用的内容。
The code I'm using to fill the Userevent, for those interested, is:
对于那些感兴趣的人,我用来填充 Userevent 的代码是:
std::string filename = "ResumeButton.png";
SDL_Event button_press;
button_press.type = BUTTON_PRESS;
button_press.user.data1 = &filename;
SDL_PushEvent(&button_press);
Any ideas?
有任何想法吗?
EDIT: Thanks for all the responses, I just needed to cast the void* to a std::string*. Silly me. Thank you guys so much!
编辑:感谢所有回复,我只需要将 void* 转换为 std::string*。傻我。非常感谢你们!
采纳答案by Stephen
You just need to dynamically allocate it (because it probably needs to outlive the scope you're using it in), then cast it back and forth:
您只需要动态分配它(因为它可能需要比您使用它的范围更长),然后来回转换:
// Cast a dynamically allocated string to 'void*'.
void *vp = static_cast<void*>(new std::string("it's easy to break stuff like this!"));
// Then, in the function that's using the UserEvent:
// Cast it back to a string pointer.
std::string *sp = static_cast<std::string*>(vp);
// You could use 'sp' directly, or this, which does a copy.
std::string s = *sp;
// Don't forget to destroy the memory that you've allocated.
delete sp;
回答by R Samuel Klatchko
Based on your comment "What I meant was to convert what the void* is pointing to (which is a string) into a string."
根据您的评论“我的意思是将 void* 指向的内容(字符串)转换为字符串。”
Assuming you have this:
假设你有这个:
std::string str = ...;
void *ptr = &str;
You can just cast back to the string:
你可以只转换回字符串:
std::string *pstr = static_cast<std::string *>(ptr);
Note that it is on you to verify that ptractually points to a std::string. If you are mistaken and it points to something else, this will cause undefined behavior.
请注意,您需要验证是否ptr实际指向std::string. 如果你弄错了,它指向别的东西,这将导致未定义的行为。
回答by R Samuel Klatchko
If you trying to format the address as text you can use a stringstream:
如果您尝试将地址格式化为文本,您可以使用stringstream:
std::stringstream strm;
strm << ptr;
std::string str = strm.str();
// str will now have something like "0x80004567"
If that's not what you are interested in, please clarify your question.
如果这不是您感兴趣的内容,请澄清您的问题。
回答by Puppy
If the void is a const char*, then you can just call the std::string constructor with it, i.e.
如果 void 是一个 const char*,那么你可以用它调用 std::string 构造函数,即
const char* cakes = something;
std::string lols = std::string(cakes);

