最佳答案
转换
char* p = "foobar";
在 C++98/C++03 中被弃用,并在 C++0x 中被移除(即,§4.2/2 被移除)。
因此,该代码在 C++0x 中无效。
但是,MinGW g++ 4.4.1 仍然只发出警告,不发出错误。
C++98/C++03 §4.2/2 (which is removed in C++0x):
A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer tochar
”; a wide string literal can be converted to an rvalue of type “pointer towchar_t
”. In either case, the result is a pointer to the first element of the array. This conversion is considered only when there is an explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an rvalue. [Note: this conversion is deprecated. See Annex D. ] For the purpose of ranking in overload resolution (13.3.3.1.1), this conversion is considered an array-to-pointer conversion followed by a qualification conversion (4.4). [Example:"abc"
is converted to “pointer toconst char
” as an array-to-pointer conversion, and then to “pointer tochar
” as a qualification conversion. ]
关于c++ - 将字符串文字分配给 char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6492410/