清除 C++ 中的函数隐藏编译器警告

发布于 2024-10-31 08:41:17 字数 179 浏览 3 评论 0原文

我在编译器中收到“隐藏”警告,因为从其父级继承的类具有相同的名称但不同的参数。

添加一个仅发出警告的函数,表明该函数不执行任何操作(在基类中也是如此,但没有警告),将基类函数的参数和名称与派生类相匹配,从而清除此编译器警告。这对派生类有哪些潜在的连锁反应?

编辑:假设我不希望他们能够使用基类函数。 (不要问)。

I'm getting "hiding" warnings in my compiler because a class inherits from its parent has the same name but different parameters.

Adding a function that merely pushes out a warning to say that this function does nothing (as is true in the base class, but without the warning) that matches the parameters and name of the base class function to the derived class clears this compiler warning. What are the potential knock-on effects of this on the derived class?

EDIT: Assume that I do not wish them to be able to use the base class function. (Don't ask).

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

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

发布评论

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

评论(3

且行且努力 2024-11-07 08:41:17

在派生类中重新定义名称实际上隐藏了基类中的函数。这就是警告告诉你的! :-)

这是一个警告,因为通常这是一个错误。如果是故意的,那没关系(但非常罕见)。

Redefining the name in the derived class effectively hides the function in the base class. That's what the warning tells you! :-)

It is a warning, because usually this is a mistake. If it is on purpose, that is ok (but very rare).

围归者 2024-11-07 08:41:17

用户无法在不显式写出 myDerivedObj.Base::foo() 的情况下通过派生实例访问基类函数,而他们不太可能这样做。

相反,使您的函数签名匹配,或者更改函数名称。

The inability of your users to access the base class function through a derived instance without explicitly writing out myDerivedObj.Base::foo(), which they're not likely to do.

Make your function signatures match up, instead, or change the function name.

寒冷纷飞旳雪 2024-11-07 08:41:17

您需要在派生类中取消隐藏基类函数,如下所示

using Base::Function;

: 示例:

class Base
{
public:
   void Function(int) { cout << "Function(int)" << endl; }
};

class Derived : public Base
{
public:
   using Base::Function; //NOTE THIS LINE : Unhiding base class function!
   void Function(const char *) { cout << "Function(const char *)" << endl; }
};

Derived d;
d.Function(10); //this calls Base::Function

演示: http://ideone.com/OTBxg

You need to unhide the base class function in the derived class as:

using Base::Function;

Example:

class Base
{
public:
   void Function(int) { cout << "Function(int)" << endl; }
};

class Derived : public Base
{
public:
   using Base::Function; //NOTE THIS LINE : Unhiding base class function!
   void Function(const char *) { cout << "Function(const char *)" << endl; }
};

Derived d;
d.Function(10); //this calls Base::Function

Demo : http://ideone.com/OTBxg

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