C++ 已弃用从字符串常量到“char*”的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1524356/
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
C++ deprecated conversion from string constant to 'char*'
提问by mkamthan
I have a class with a private char str[256];
我有一个班级 private char str[256];
and for it I have an explicit constructor:
为此,我有一个显式构造函数:
explicit myClass(const char *func)
{
strcpy(str,func);
}
I call it as:
我称之为:
myClass obj("example");
When I compile this I get the following warning:
当我编译它时,我收到以下警告:
deprecated conversion from string constant to 'char*'
不推荐将字符串常量转换为 'char*'
Why is this happening?
为什么会这样?
采纳答案by sellibitze
This is an error message you see whenever you have a situation like the following:
这是您在遇到以下情况时看到的错误消息:
char* pointer_to_nonconst = "string literal";
Why? Well, C and C++ differ in the type of the string literal. In C the type is array of char and in C++ it is constantarray of char. In any case, you are not allowed to change the characters of the string literal, so the const in C++ is not really a restriction but more of a type safety thing. A conversion from const char*
to char*
is generally not possible without an explicit cast for safety reasons. But for backwards compatibility with C the language C++ still allows assigning a string literal to a char*
and gives you a warning about this conversion being deprecated.
为什么?好吧,C 和 C++ 在字符串文字的类型上有所不同。在 C 中,类型是 char 数组,在 C++ 中,它是char常量数组。在任何情况下,都不允许更改字符串字面量的字符,因此 C++ 中的 const 并不是真正的限制,而是更多的类型安全。出于安全原因,如果没有显式强制转换,从const char*
to的转换char*
通常是不可能的。但是为了与 C 向后兼容,C++ 语言仍然允许将字符串文字分配给 achar*
并警告您此转换已被弃用。
So, somewhere you are missing one or more const
s in your program for const correctness. But the code you showed to us is not the problem as it does not do this kind of deprecated conversion. The warning must have come from some other place.
因此,const
为了 const 的正确性,您在程序中的某处缺少一个或多个s。但是您向我们展示的代码不是问题,因为它没有进行这种已弃用的转换。警告一定来自其他地方。
回答by fnieto - Fernando Nieto
The warning:
警告:
deprecated conversion from string constant to 'char*'
不推荐将字符串常量转换为 'char*'
is given because you are doing somewhere (not in the code you posted) something like:
给出是因为您正在某处(不在您发布的代码中)执行以下操作:
void foo(char* str);
foo("hello");
The problem is that you are trying to convert a string literal (with type const char[]
) to char*
.
问题是您正在尝试将字符串文字(带有 type const char[]
)转换为char*
.
You can convert a const char[]
to const char*
because the array decays to the pointer, but what you are doing is making a mutable a constant.
您可以将 a 转换为const char[]
,const char*
因为数组衰减为指针,但您所做的是将可变变量设为常量。
This conversion is probably allowed for C compatibility and just gives you the warning mentioned.
这种转换可能是为了 C 兼容性而允许的,只是给你提到的警告。
回答by sactiw
As answer no. 2 by fnieto - Fernando Nietoclearly and correctly describes that this warning is given because somewhere in your code you are doing (not in the code you posted) something like:
作为答案没有。2 by fnieto - Fernando Nieto清楚且正确地描述了给出此警告是因为在您的代码中您正在执行的某处(而不是您发布的代码中)类似于:
void foo(char* str);
foo("hello");
However, if you want to keep your code warning-free as well then just make respective change in your code:
但是,如果您还想保持您的代码无警告,那么只需在您的代码中进行相应的更改:
void foo(char* str);
foo((char *)"hello");
That is, simply cast the string
constant to (char *)
.
也就是说,只需将string
常量强制转换为(char *)
。
回答by anilbey
There are 3 solutions:
有3种解决方案:
Solution 1:
解决方案1:
const char *x = "foo bar";
Solution 2:
解决方案2:
char *x = (char *)"foo bar";
Solution 3:
解决方案3:
char* x = (char*) malloc(strlen("foo bar")+1); // +1 for the terminator
strcpy(x,"foo bar");
Arrays also can be used instead of pointers because an array is already a constant pointer.
数组也可以用来代替指针,因为数组已经是一个常量指针。
回答by dan ionescu
In fact a string constant literal is neither a const char * nor a char* but a char[]. Its quite strange but written down in the c++ specifications; If you modify it the behavior is undefined because the compiler may store it in the code segment.
事实上,字符串常量文字既不是 const char * 也不是 char* 而是 char[]。它很奇怪,但写在 c++ 规范中;如果您修改它,则行为未定义,因为编译器可能会将其存储在代码段中。
回答by Alen Lee
Maybe you can try this:
也许你可以试试这个:
void foo(const char* str)
{
// Do something
}
foo("Hello")
It works for me
这个对我有用
回答by TWOPIR
I solve this problem by adding this macro in the beginning of the code, somewhere. Or add it in <iostream>
, hehe.
我通过在代码开头的某处添加这个宏来解决这个问题。或者加进去<iostream>
,呵呵。
#define C_TEXT( text ) ((char*)std::string( text ).c_str())
回答by Sahil Singh
A reason for this problem (which is even harder to detect than the issue with char* str = "some string"
- which others have explained) is when you are using constexpr
.
这个问题的一个原因(比char* str = "some string"
其他人解释的问题更难检测)是当您使用constexpr
.
constexpr char* str = "some string";
It seems that it would behave similar to const char* str
, and so would not cause a warning, as it occurs before char*
, but it instead behaves as char* const str
.
似乎它的行为类似于const char* str
,因此不会像之前发生的那样引起警告,char*
而是表现为char* const str
。
Details
细节
Constant pointer, and pointer to a constant.The difference between const char* str
, and char* const str
can be explained as follows.
常量指针,以及指向常量的指针。const char* str
、 和之间的区别char* const str
可以解释如下。
const char* str
: Declare str to be a pointer to a const char. This means that the data to which this pointer is pointing to it constant. The pointer can be modified, but any attempt to modify the data would throw a compilation error.str++ ;
: VALID. We are modifying the pointer, and not the data being pointed to.*str = 'a';
: INVALID. We are trying to modify the data being pointed to.
char* const str
: Declare str to be a const pointer to char. This means that point is now constant, but the data being pointed too is not. The pointer cannot be modified but we can modify the data using the pointer.str++ ;
: INVALID. We are trying to modify the pointer variable, which is a constant.*str = 'a';
: VALID. We are trying to modify the data being pointed to. In our case this will not cause a compilation error, but will cause a runtime error, as the string will most probably will go into a read only section of the compiled binary. This statement would make sense if we had dynamically allocated memory, eg.char* const str = new char[5];
.
const char* const str
: Declare str to be a const pointer to a const char. In this case we can neither modify the pointer, nor the data being pointed to.str++ ;
: INVALID. We are trying to modify the pointer variable, which is a constant.*str = 'a';
: INVALID. We are trying to modify the data pointed by this pointer, which is also constant.
const char* str
: 将 str 声明为指向 const char 的指针。这意味着该指针指向的数据是常量。可以修改指针,但任何修改数据的尝试都会引发编译错误。str++ ;
:有效。我们正在修改指针,而不是被指向的数据。*str = 'a';
:无效。我们正在尝试修改所指向的数据。
char* const str
: 将 str 声明为指向 char 的 const 指针。这意味着该点现在是常数,但指向的数据也不是。指针无法修改,但我们可以使用指针修改数据。str++ ;
:无效。我们试图修改指针变量,它是一个常量。*str = 'a';
:有效。我们正在尝试修改所指向的数据。在我们的例子中,这不会导致编译错误,但会导致运行时错误,因为字符串很可能会进入已编译二进制文件的只读部分。如果我们动态分配了内存,则此语句将有意义,例如。char* const str = new char[5];
.
const char* const str
: 将 str 声明为指向 const char 的 const 指针。在这种情况下,我们既不能修改指针,也不能修改所指向的数据。str++ ;
:无效。我们试图修改指针变量,它是一个常量。*str = 'a';
:无效。我们试图修改这个指针指向的数据,它也是常量。
In my case the issue was that I was expecting constexpr char* str
to behave as const char* str
, and not char* const str
, since visually it seems closer to the former.
就我而言,问题是我期望constexpr char* str
表现为const char* str
,而不是char* const str
,因为在视觉上它似乎更接近前者。
Also, the warning generated for constexpr char* str = "some string"
is slightly different from char* str = "some string"
.
此外,为 生成的警告与constexpr char* str = "some string"
略有不同char* str = "some string"
。
- Compiler warning for
constexpr char* str = "some string"
:ISO C++11 does not allow conversion from string literal to 'char *const'
- Compiler warning for
char* str = "some string"
:ISO C++11 does not allow conversion from string literal to 'char *'
.
- 编译器警告
constexpr char* str = "some string"
:ISO C++11 does not allow conversion from string literal to 'char *const'
- 编译器警告
char* str = "some string"
:ISO C++11 does not allow conversion from string literal to 'char *'
。
Tip
提示
You can use C gibberish ? English converterto convert C
declarations to easily understandable English statements, and vice versa. This is a C
only tool, and thus wont support things (like constexpr) which are exclusive to C++
.
你可以用C 胡言乱语?英文转换器将C
声明转换为易于理解的英文语句,反之亦然。这是C
唯一的工具,因此不会支持C++
.
回答by dilantha111
I also got the same problem. And what I simple did is just adding const char* instead of char*. And the problem solved. As others have mentioned above it is a compatible error. C treats strings as char arrays while C++ treat them as const char arrays.
我也遇到了同样的问题。而我所做的只是添加 const char* 而不是 char*。问题解决了。正如其他人在上面提到的,这是一个兼容错误。C 将字符串视为 char 数组,而 C++ 将它们视为 const char 数组。
回答by bremen_matt
For what its worth, I find this simple wrapper class to be helpful for converting C++ strings to char *
:
就其价值而言,我发现这个简单的包装类有助于将 C++ 字符串转换为char *
:
class StringWrapper {
std::vector<char> vec;
public:
StringWrapper(const std::string &str) : vec(str.begin(), str.end()) {
}
char *getChars() {
return &vec[0];
}
};