简体   繁体   English

在派生类中重载基类方法

[英]overloading base class method in derived class

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.我试图理解为什么以下代码无法编译,显然该解决方案依赖于在派生类中专门声明对 method_A 的依赖。 Please refer to the following code:请参考以下代码:

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.我正在寻找更好地理解编译器/链接器在这种情况下的行为方式。

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 Base class 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 this for better explanation why name hiding came into picture.查看这个以获得更好的解释为什么名称隐藏会出现。

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.其次,您没有覆盖任何内容,因为 method_A 不是虚拟的。 You're overloading.你超载了。

If your intention is to override void 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 almost the 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 overloading the function in the base class.您当前所做的就是重载基类中的函数。 The using keyword is bringing the base class function into the child class' namespace, as the language behaviour is not to do this by default. using关键字将基类函数带入子类的命名空间,因为默认情况下语言行为不这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM