C++ deprecated conversion from string constant to 'char*'

前端 未结 11 1179
傲寒
傲寒 2020-11-22 16:01

I have a class with a private char str[256];

and for it I have an explicit constructor:

explicit myClass(const char *func)
{
    strcpy(         


        
11条回答
  •  有刺的猬
    2020-11-22 16:55

    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.

提交回复
热议问题