c ++重载的虚函数警告? [英] c++ overloaded virtual function warning by clang?

查看:1309
本文介绍了c ++重载的虚函数警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

clang在编译以下代码时发出警告:

clang emits a warning when compiling the following code:

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]


$ b b

(当然需要启用所述警告)。

(the said warning needs to be enabled of course).

我不明白为什么。注意,在Base中取消注释相同的声明会关闭警告。我的理解是,由于两个get()函数有不同的签名,所以不能隐藏。

I don't understand why. Note that uncommenting the same declaration in Base shuts the warning up. My understanding is that since the two get() functions have different signatures, there can be no hiding.

是clang吧?为什么?

Is clang right? Why?

注意这是在MacOS X上,运行最新版本的Xcode。

Note this is on MacOS X, running a recent version of Xcode.

clang --version
Apple LLVM version 5.0 (clang-500.1.74) (based on LLVM 3.3svn)

更新:与Xcode 4.6.3相同的行为。

Update: same behavior with Xcode 4.6.3.

推荐答案

此警告是为了防止在意图覆盖时意外隐藏过载。考虑一个稍微不同的例子:

This warning is there to prevent accidental hiding of overloads when overriding is intended. Consider a slightly different example:

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
};

由于它是一个警告,它不一定意味着它是一个错误,一。通常这样的警告有一种方法,通过更明确的关闭它们,让编译器知道你打算你写的。我相信在这种情况下,您可以执行以下操作:

As it is a warning, it doesn't necessarily mean it is a mistake, but it might indicate one. Usually such warnings have a means of shutting them off by being more explicit and letting the compiler know you did intend what you wrote. I believe in this case you can do the following:

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);
};

这篇关于c ++重载的虚函数警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆