clang 在编译以下代码时发出警告:
struct Base
{
virtual void * get(char* e);
// virtual void * get(char* e, int index);
};
struct Derived: public Base {
virtual void * get(char* e, int index);
};
警告是:
warning: 'Derived::get' hides overloaded virtual function [-Woverloaded-virtual]
(当然需要启用上述警告)。
我不明白为什么。请注意,在 Base 中取消注释相同的声明会关闭警告。我的理解是,由于两个 get() 函数具有不同的签名,因此无法隐藏。
叮当是对的吗?为什么?
请注意,这是在 MacOS X 上运行最新版本的 Xcode。
clang --version
Apple LLVM version 5.0 (clang-500.1.74) (based on LLVM 3.3svn)
更新:与 Xcode 4.6.3 的行为相同。
此警告是为了防止在打算覆盖时意外隐藏过载。考虑一个稍微不同的例子:
struct chart; // let's pretend this exists
struct Base
{
virtual void* get(char* e);
};
struct Derived: public Base {
virtual void* get(chart* e); // typo, we wanted to override the same function
};
由于它是一个警告,并不一定意味着它是一个错误,但它可能表明一个错误。通常,此类警告有一种方法可以通过更明确地关闭它们,并让编译器知道您确实想写什么。我相信在这种情况下您可以执行以下操作:
struct Derived: public Base {
using Base::get; // tell the compiler we want both the get from Base and ours
virtual void * get(char* e, int index);
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)