将 void* 转换为 std::string

发布于 2024-09-06 10:59:47 字数 616 浏览 7 评论 0 原文

在仔细阅读网络并搞乱自己之后,我似乎无法将 void* 的目标(这是一个字符串)转换为 std::string 。我尝试过按照 /thread221261.html" rel="noreferrer">此页面 获取 C 字符串,但无济于事。遗憾的是,是的,我必须使用 void*,因为这就是 SDL 在其 USEREVENT 结构中使用的内容。

对于那些感兴趣的人,我用来填充 Userevent 的代码是:

std::string filename = "ResumeButton.png";
SDL_Event button_press;
button_press.type = BUTTON_PRESS;
button_press.user.data1 = &filename;
SDL_PushEvent(&button_press);

有什么想法吗?

编辑:感谢所有回复,我只需要将 void* 转换为 std::string* 。愚蠢的我。非常感谢你们!

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 page to 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.

The code I'm using to fill the Userevent, for those interested, is:

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吖咩 2024-09-13 10:59:47

您只需要动态分配它(因为它可能需要比您使用它的范围更长),然后来回转换它:

// 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;

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;
橘和柠 2024-09-13 10:59:47

根据您的评论“我的意思是将 void* 指向的内容(这是一个字符串)转换为字符串。”

假设您有这样的情况:

std::string str = ...;
void *ptr = &str;

您可以直接转换回字符串:

std::string *pstr = static_cast<std::string *>(ptr);

请注意,您需要验证 ptr 是否确实指向 std::string。如果你弄错了并且它指向其他东西,这将导致未定义的行为。

Based on your comment "What I meant was to convert what the void* is pointing to (which is a string) into a string."

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 ptr actually points to a std::string. If you are mistaken and it points to something else, this will cause undefined behavior.

戴着白色围巾的女孩 2024-09-13 10:59:47

如果您尝试将地址格式化为文本,则可以使用stringstream

std::stringstream strm;
strm << ptr;
std::string str = strm.str(); 

// str will now have something like "0x80004567"

如果这不是您感兴趣的内容,请澄清您的问题。

If you trying to format the address as text you can use a 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.

软的没边 2024-09-13 10:59:47

如果 void 是 const char*,那么您可以用它调用 std::string 构造函数,即

const char* cakes = something;
std::string lols = std::string(cakes);

If the void is a const char*, then you can just call the std::string constructor with it, i.e.

const char* cakes = something;
std::string lols = std::string(cakes);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文