0
4

2 回答 2

1

A string constant is, as the name says, a constant. So to make the warning go away, the proper solution would indeed be to turn the pointer into a const pointer.

If that cannot be done, you can explicit typecasts

ACE_TCHAR* argv[] = {const_cast<ACE_TCHAR*>("Input1"),
                     const_cast<ACE_TCHAR*>("Input2")};

or assign the strings to non-constant char arrays first

char input1[] = "Input1";
char input2[] = "Input2";
ACE_TCHAR* argv[] = {input1, input2};
于 2013-02-01T14:36:42.570 回答
1

The type of a string literal is "array of const char". By the implicit array-to-pointer conversion, you can use this to initialize or assign to a const char *.

But there is a special rule, that a string literal can also be implicitly converted to a char * (without const). This rule exists for compatibility with old C code, where char * str = "string literal" was a common idiom. Using this is dangerous, because modifying the pointed to character array through that pointer causes undefined behavior (i.e. your program may crash, or anything else can happen). For that reason the construct is deprecated and your compiler warns you about it.

To create valid data you can pass on as non-const character pointers, you can use

const int argc = 2;
ACE_TCHAR argv0[] = "Input1";
ACE_TCHAR argv1[] = "Input2";
ACE_TCHAR* argv[] = { argv0, argv1 };
MyClass *myClass = new MyClass(argc, argv);
于 2013-02-01T14:38:24.150 回答