如何将 std::string 传递给需要 char* 的函数? [英] How do I pass an std::string to a function that expects char*?

查看:68
本文介绍了如何将 std::string 传递给需要 char* 的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
我可以得到一个非常量 C从 C++ 字符串返回字符串?

我需要先转换吗?我在另一篇文章中看到,如果函数需要 const char*,则可以使用 .c_str().仅使用 char* 怎么样?

Do I need to convert it first? I saw in another post that .c_str() can be used if the function expected const char*. What about for just char*?

推荐答案

没有办法从保证在所有平台上工作的字符串中获取字符*,因为简单的事实是string 不需要使用连续存储.

There is no way to get a char* from a string that is guaranteed to work on all platforms, for the simple fact that string is not required to use contiguous storage.

最安全、最便携的做法是将字符串复制到确实使用连续存储(可能是 vector)的某个位置,然后使用它.

Your safest, most portable course of action is to copy the string somewhere that does use contigious storage (a vector perhaps), and use that instead.

vector<char> chars(my_string.begin(), my_string.end());
char* ptr = &chars[0];

如果您想变得笨拙、不可移植且绝对不安全,您可以确认您的 string 实现确实使用了连续存储,然后也许可以使用:

If you want to be hacky and non-portable and decidedly unsafe, you can confirm that your string implementation does in fact use contigious storage, and then maybe use this:

&my_str[0]

但我会打任何为我工作的开发人员这样做.

But I would punch any developer that worked for me that did this.

我知道目前没有已知的 STL 实现不将字符串数据存储在连续数组中,这将使 &my_str[0] 安全.在即将发布的 C++0x 标准中,也确实(我被要求说明这一点)要求存储是连续的.

I've been made aware that there are currently no known STL implementations that do not store the string data in a contiguous array, which would make &my_str[0] safe. It is also true (and I was asked to state this) that in the upcoming C++0x standard, it will be required for the storage to be contiguous.

有人建议,因为如果这些事实表明我的帖子实际上是不正确的.

It's been suggested that because if these facts that my post is factually incorrect.

你自己决定,但我拒绝.这不在当前 C++ 标准中,因此不是必需的.在实践中,我仍然会按照我建议的方式做事,并且在任何代码审查中,我都会标记任何假设底层存储是连续的代码.

Decide for yourself, but I say no. This is not in the current C++ standard, and so it is not required. I will still in practice do things the way I have suggested, and in any code review I will flag any code that assumes the underlying storage is contigious.

考虑一下.假设有一个关于 vtable 指针的问题.有人想检查一个类并通过查看 vtable 来获取指向虚函数的指针.我会立即告诉他们不要这样做,因为没有提到 虚拟方法是如何在 C++ 中实现的.我知道的每个实现都使用 vtables,我想不出更好的方法来做到这一点.很可能多态性将永远使用 vtables 实现.这样可以直接检查 vtable 吗?

Consider this. Suppose there were a question about vtable pointers. Someone wants to examing a class and get the pointer to a virtual function by looking at the vtable. I would immediately tell them not to do this because there is no mention of how virtual methods are implemented in C++. Every implementation I know uses vtables, and I can't think of a better way to do it. It is likely that polymorphism will forever be implemented using vtables. Does that make it ok to examing the vtable directly?

IMO 否,因为这取决于未记录的实施细节.你无法控制它,它随时可能改变.即使你期望它永远不会改变,依赖这些实现细节仍然是糟糕的工程.

IMO no, because this depends on undocumented implementation details. You have no control over this, and it could change at any time. Even if you expect it will never change, it is still bad engineering to rely on these implementation details.

自己决定.

这篇关于如何将 std::string 传递给需要 char* 的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆