可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a class with a 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*\'
Why is this happening?
回答1:
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 constant array 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.
So, somwehere 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.
回答2:
The warning:
deprecated conversion from string constant to \'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*
.
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.
This conversion is probably allowed for C compatibility and just gives you the warning mentioned.
回答3:
As answer no. 2 by fnieto - Fernando Nieto clearly and correctly describes that this warning is given because somewhere in your code you are doing (not in the code you posted) something like:
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 *)
.
回答4:
There are 3 solutions:
Solution 1:
const char *x = \"foo bar\";
Solution 2:
char *x = (char *)\"foo bar\";
Solution 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.
回答5:
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.
回答6:
I solve this problem by adding this macro in the beginning of the code, somewhere. Or add it in <iostream>
, hehe.
#define C_TEXT( text ) ((char*)std::string( text ).c_str())
回答7:
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.
回答8:
For what its worth, I find this simple wrapper class to be helpful for converting C++ strings to char *
:
class StringWrapper {
std::vector<char> vec;
public:
StringWrapper(const std::string &str) : vec(str.begin(), str.end()) {
}
char *getChars() {
return &vec[0];
}
};
回答9:
Maybe you can try this:
void foo(const char* str)
{
// Do something
}
foo(\"Hello\")
It works for me
回答10:
The worst part about this is the typical ambiguity in the abuse of the reserved word \"string,\" which leads one to believe, wrongly, that \"example\".c_str() would resolve the issue.
回答11:
The following illustrates the solution, assign your string to a variable pointer to a constant array of char (a string is a constant pointer to a constant array of char - plus length info):
#include <iostream>
void Swap(const char * & left, const char * & right) {
const char *const temp = left;
left = right;
right = temp;
}
int main() {
const char * x = \"Hello\"; // These works because you are making a variable
const char * y = \"World\"; // pointer to a constant string
std::cout << \"x = \" << x << \", y = \" << y << \'\\n\';
Swap(x, y);
std::cout << \"x = \" << x << \", y = \" << y << \'\\n\';
}