Should useless type qualifiers on return types be used, for clarity?

后端 未结 6 1209
有刺的猬
有刺的猬 2020-11-30 09:05

Our static analysis tool complains about a \"useless type qualifier on return type\" when we have prototypes in header files such as:

const int foo();
         


        
6条回答
  •  借酒劲吻你
    2020-11-30 09:47

    const int foo() is very different from const char* foo(). const char* foo() returns an array (usually a string) whose content is not allowed to change. Think about the difference between:

     const char* a = "Hello World";
    

    and

    const int b = 1;
    

    a is still a variable and can be assigned to other strings that can't change whereas b is not a variable. So

    const char* foo();
    const char* a = "Hello World\n";
    a = foo();
    

    is allowed but

    const int bar();
    const int b = 0;
    b = bar();
    

    is not allowed, even with the const declaration of bar().

提交回复
热议问题