C++ 已弃用从字符串文字到 'char*' 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9650058/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:07:46  来源:igfitidea点击:

Deprecated conversion from string literal to 'char*'

c++stringchardeprecatedliterals

提问by Matt

I have a program which declares an array of strings like this:

我有一个程序,它声明了一个这样的字符串数组:

char *colors[4] = {"red", "orange", "yellow", "blue"};

But I get the above compiler warning. It compiles but I'd rather use the non-deprecated way(if there is one). I've tried to find out what it means, but I can't seem to figure it out. I've heard using 'const' before 'char' works, but it would be helpful if someone could explain what the error means. Thanks.

但是我收到了上面的编译器警告。它可以编译,但我宁愿使用未弃用的方式(如果有的话)。我试图找出它的意思,但我似乎无法弄清楚。我听说在 'char' 工作之前使用了 'const',但如果有人能解释错误的含义,那将会很有帮助。谢谢。

回答by d_inevitable

The strings that you enter: "red", "organge" etc are "literal", because they are defined inside the program code itself (they are not read directly from disk, user input /stdin etc.).

您输入的字符串:“red”、“organge”等是“文字”,因为它们是在程序代码本身中定义的(它们不是直接从磁盘、用户输入 /stdin 等读取的)。

This means that if at any point you try to write to your colorsyou will be directly accessing your original input and thus editing it. This would cause some undesired run-time errors.

这意味着,如果您在任何时候尝试写信给您,colors您将直接访问您的原始输入并对其进行编辑。这会导致一些不希望的运行时错误。

Declaring it as a const will make sure that you will never try to write to this pointer and such a run-time error can be avoided.

将其声明为 const 将确保您永远不会尝试写入此指针,并且可以避免此类运行时错误。

const char *colors[4] = {"red", "orange", "yellow", "blue"};

If you ever feel like editing these values at runtime, then you should copy the strings first.

如果您想在运行时编辑这些值,那么您应该先复制这些字符串。

回答by B?ови?

"red", "orange", "yellow", "blue"

these are constant string. Creating a non-const pointer to a constant string is wrong, hence the warning. At the moment you are getting a warning, but it should be an error since it is deprecated in c++03, and forbiden in c++11.

这些是常量字符串。创建一个指向常量字符串的非常量指针是错误的,因此是警告。目前您收到警告,但它应该是一个错误,因为它在 c++03 中被弃用,在 c++11 中被禁止。

回答by tiktak

These answers are all correct.

这些答案都是正确的。

Note that if you have a function requiring an array of characters as an argument and you pass this argument like this:

请注意,如果您有一个需要字符数组作为参数的函数,并且您像这样传递这个参数:

foo ("bar");

the same warning will be shown. In this case, you can either :

将显示相同的警告。在这种情况下,您可以:

1) Change it like this, as explained in the first answer:

1)像这样改变它,如第一个答案中所述:

void foo (char[] str) { printf(str); }

const char param[] = "bar";
foo (param);

2) Consider using a C++ standard string, like so:

2) 考虑使用 C++ 标准字符串,如下所示:

void foo (std::string theParam) { std::cout << theParam; }

foo ("bar");

IMHO, as long as no real performance issue is concerned and you are not working with C libraries, or if you are building a C++ library for others to use, you should rather work with C++ immutable strings and their feature set.

恕我直言,只要不涉及真正的性能问题并且您不使用 C 库,或者如果您正在构建 C++ 库供其他人使用,您应该使用 C++ 不可变字符串及其功能集。

If Unicode is a requirement, the support in C++ is "terrible" as explained here. This questiongives you some clues (mainly: use IBM ICU library). If you already have Qt in your project, QStringwill also do the trick, and so will Gettext.

如果Unicode是必需的,在C ++的支持是“可怕”的解释在这里这个问题给了你一些线索(主要是:使用IBM ICU库)。如果你的项目中已经有 Qt,它QString也可以解决这个问题,Gettext 也是如此。