编译器警告(级别 3 和级别 4)C4101
“identifier”:未引用的局部变量
从不使用局部变量。 此警告出现在明显的情况下:
// C4101a.cpp
// compile with: /W3
int main() {
int i; // C4101
}
但是,通过类的实例调用 static
成员函数时也会出现此警告:
// C4101b.cpp
// compile with: /W3
struct S {
static int func()
{
return 1;
}
};
int main() {
S si; // C4101, si is never used
int y = si.func();
return y;
}
在这种情况下,编译器使用有关 si
的信息来访问 static
函数,但调用 static
函数不需要类的实例;因此会出现警告。 可通过执行以下操作解决此警告:
添加构造函数,编译器将在其中使用
si
的实例调用func
。从
func
的定义中删除static
关键字。显式调用
static
函数:int y = S::func();
。