C++ 在派生类中重载基类方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16835897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:41:16  来源:igfitidea点击:

overloading base class method in derived class

c++oopoverloading

提问by dau_sama

I am trying to understand why the following code does not compile, apparently the solution relies in specifically declaring the dependency on method_A in the derived class. Please refer to the following code:

我试图理解为什么以下代码无法编译,显然该解决方案依赖于在派生类中专门声明对 method_A 的依赖。请参考以下代码:

class Base
{
  public:

    void method_A(int param, int param2)
    {
      std::cout << "Base call A" << std::endl;
    }

};

//does not compile
class Derived : public Base
{
  public:

    void method_A(int param)
    {
      std::cout << "Derived call A" << std::endl;
    }
};

//compiles
class Derived2 : public Base
{
  public:
    using Base::method_A; //compile
    void method_A(int param)
    {
      std::cout << "Derived call A" << std::endl;
    }
};

int main ()
{
  Derived myDerived;
  myDerived.method_A(1);
  myDerived.method_A(1,2);

  Derived2 myDerived2;
  myDerived2.method_A(1);
  myDerived2.method_A(1,2);
  return 0;
}

"test.cpp", (S) The wrong number of arguments have been specified for "Derived::method_A(int)".

"test.cpp", (S) 为 "Derived::method_A(int)" 指定了错误数量的参数。

What is the technical reason that prevents the derived class to know its base class is implementing the method it's trying to overload? I am looking in understanding better how the compiler/linker behaves in this case.

阻止派生类知道其基类正在实现它试图重载的方法的技术原因是什么?我正在寻找更好地理解编译器/链接器在这种情况下的行为方式。

回答by Daemon

Its called Name Hiding.When you define a non virtual method with the same name as Base method it hides the Base class method in Derived class so you are getting the error for

它被称为名称隐藏。当您定义与 Base 方法同名的非虚拟方法时,它会隐藏派生类中的 Base 类方法,因此您会收到以下错误

 myDerived.method_A(1,2);

To avoid hiding of Baseclass methods in Derived class use using keyword as you did in Derived2 class.

为避免在派生类中隐藏基类方法,请像在派生2 类中一样使用关键字。

Also if you want to make it work you can do it explictly

另外,如果你想让它工作,你可以明确地做到

myDerived.Base::method_A(1,2);

Check out thisfor better explanation why name hiding came into picture.

查看这个以获得更好的解释为什么名称隐藏会出现。

回答by Armen Tsirunyan

Well, for one you're calling

好吧,对于你要打电话的人

 myDerived.method_A(1,2);

with 2 arguments, whereas both in base and derived the method is declared to take only one argument.

有 2 个参数,而无论是在 base 中还是在 Derived 中,该方法都被声明为只接受一个参数。

Secodnly, you're not overriding anything, because method_A is not virtual. You're overloading.

其次,您没有覆盖任何内容,因为 method_A 不是虚拟的。你超载了。

回答by Bathsheba

If your intention is to overridevoid Base::method_A(int param, int param2)then you should mark it virtual in the base class:

如果您打算覆盖void ,Base::method_A(int param, int param2)那么您应该在基类中将其标记为 virtual :

 virtual void method_A(int param, int param2)

Any function overriding this must have the same parameters and almostthe same return type ('almost' loosely meaning that the differing return types must be polymorphically related, but in most cases it should have the identical return type).

任何覆盖 this 的函数都必须具有相同的参数和几乎相同的返回类型(“几乎”意味着不同的返回类型必须是多态相关的,但在大多数情况下,它应该具有相同的返回类型)。

All you're currently doing is overloadingthe function in the base class. The usingkeyword is bringing the base class function into the child class' namespace, as the language behaviour is not to do this by default.

您当前所做的就是重载基类中的函数。该using关键字带来的是基类的功能到子类的命名空间,因为语言行为不是在默认情况下做到这一点。