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

前端 未结 11 1238
傲寒
傲寒 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:53

    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 *).

提交回复
热议问题