对于隐藏具有类似原型的非虚拟方法没有警告(G++ 4.4)

发布于 2024-12-17 04:14:43 字数 467 浏览 1 评论 0原文

在我们的项目中,会发生(很少但确实发生)在派生类中,基类中的非虚拟方法被具有相同原型的方法隐藏。在这种情况下,编译器(在我们的例子中是 g++ 4.4)保持安静。虽然我可以看到没有警告对私有方法有用,但对于受保护或公共方法,这至少应该是一个可配置的警告。

如果存在这样的东西,我无法找到它。

这是一个我想让 g++ 抱怨的小例子(请放心,这种代码模式永远不会一次性写成这样,通常工作在某个时间点是 A 中的虚拟方法,后来莫名其妙地改变了):

class A
{
public:
    void work(int p)
    { /* do something */ }
};

class B : public A
{
public:
    void work(int p) 
    { /* do something different */ }
};

结果:即使使用 -Wall -Wextra 也没有警告。

In our projects it happens (rarely but it happens) that in a derived class a non-virtual method from the base-class is hidden by a method with the same prototype. In that case the compiler (in our case g++ 4.4) stays quiet. While I can see that no warning can be useful for private methods, for protected or public methods this should be at least a configurable warning.

If such a things exists I'm unable to find it.

Here's a small example I'd like to have g++ complain about (be assured that this kind of code-pattern is never written like this in one shot, usually work was at some point in time a virtual method in A and was inexplicably changed later):

class A
{
public:
    void work(int p)
    { /* do something */ }
};

class B : public A
{
public:
    void work(int p) 
    { /* do something different */ }
};

Result: no warning even with -Wall -Wextra.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶气 2024-12-24 04:14:43

您没有重写该方法,而是隐藏了它。这是一个 C++ 功能。

您可以查看此链接

另外,摘录一段有趣的内容:

注意:警告不是标准的一部分,因此您的编译器可能或
可能不会给出上述警告。

You're not overriding the method, you're hiding it. It's a C++ feature.

You can take a look at this link.

Also, an interesting extract:

Note: warnings are not part of the standard, so your compiler may or
may not give the above warning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文