C ++如果在函数中重新声明成员变量,则警告 [英] C++ Warning if re-declaring member variable in function
问题描述
给定以下结构
class A {
int test;
void f() { int test; }
}
我刚刚遇到一个奇怪的情况,当提到测试时,编译在VS2010下,正确引用函数局部变量,但是,当编译在gcc下时,不正确地引用成员变量。花了很多时间跟踪。
I just had a curious case in which the code in f(), when referring to test, compiled under VS2010, correctly referred to the function local variable, however, when compiled under gcc, incorrectly referred to the member variable. Took me quite a while to track down.
无论如何,问题是,在gcc或VS中有一个选项,以便在每次重新声明成员变量时启用编译器警告在局部函数范围内?
Anyway, question is, is there an option in gcc or VS to enable compiler warnings every time a member variable is re-declared in a local function scope?
推荐答案
在GCC中, -Wshadow
。从文档:
每当局部变量或类型声明影响另一个
变量,参数,类型或类成员(在C ++中)或每当
内置函数被遮蔽。注意,在C ++中,如果一个局部变量影子结构/类/枚举,编译器不会警告
,但是如果
隐藏了一个显式的typedef,则会发出警告。
Warn whenever a local variable or type declaration shadows another variable, parameter, type, or class member (in C++), or whenever a built-in function is shadowed. Note that in C++, the compiler will not warn if a local variable shadows a struct/class/enum, but will warn if it shadows an explicit typedef.
这篇关于C ++如果在函数中重新声明成员变量,则警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!