当派生类成员变量名称影响其父类之一时,有没有办法生成警告,例如
class Mother
{
public:
Mother() : i(0) {}
virtual ~Mother() {}
protected:
int i;
};
class Child : public Mother
{
public:
Child() : Mother(), i(0) {}
virtual ~Child() {}
protected:
int i; /* NOK Expecting warning : declaration of 'int Child::i' shadows 'int Mother::i' */
};
Run Code Online (Sandbox Code Playgroud)
-Wshadow使用g ++ 编译时,上面的代码不会生成警告.
以下代码片段有一个内存泄漏,我花了太多时间追逐.问题是在Foo()内部,局部变量x_隐藏了成员变量x_.它也很烦人,因为编译器可能已经警告过我.海湾合作委员会是否有这样的警告标志?(对于好奇:我已经通过首先使用局部变量,然后将其更改为成员变量,但忘记删除类型声明来到达错误代码.)
struct A {
A() x_(NULL) {}
~A() {
delete x_;
}
void Foo() {
HugeThingy* x_ = new HugeThingy();
x_->Bar("I. Need. Garbage. Collection. Now.");
}
HugeThingy* x_;
DISALLOW_COPY_AND_ASSIGN(A); // Macro to prevent copy/assign.
}
Run Code Online (Sandbox Code Playgroud)