当我们在头文件中有原型时,我们的静态分析工具会抱怨"返回类型上的无用类型限定符",例如:
const int foo();
Run Code Online (Sandbox Code Playgroud)
我们这样定义它是因为函数返回一个永远不会改变的常量,认为API似乎更清晰const.
我觉得这类似于为了清楚明确地将全局变量初始化为零,即使C标准已经声明如果未明确初始化所有全局变量将被初始化为零.在一天结束时,它真的没关系.(但静态分析工具并没有抱怨.)
我的问题是,有什么理由可以导致问题吗?我们是否应该忽略该工具产生的错误,或者我们是否应该以不太清晰和一致的API的可能成本来安抚该工具?(它返回const char*该工具没有问题的其他常量.)
///////////////////////////////////////
class A {
...
const double funA(void)
{...}
};
A a;
double x = a.funA();
// although the intention is to
// enforce the return value to be const and cannot be
// modified, it has little effect in the real world.
class A2 {
...
double funB(void)
{...}
};
///////////////////////////////////////
class A {
void setA(const double d)
{ // now you cannot change the value of d, so what?
// From my point of view, it is NOT …Run Code Online (Sandbox Code Playgroud)