为什么我需要重新声明重载的虚函数?

Why do I need to redeclare overloaded virtual functions?(为什么我需要重新声明重载的虚函数?)
本文介绍了为什么我需要重新声明重载的虚函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个重载函数 f(void) 和 f(int) 的基类.Derived 类通过调用 f(void) 实现 f(int).Derived2 仅实现 f(void).

I have a base class with two overloaded functions f(void) and f(int). The class Derived implements f(int) by calling f(void). Derived2 implements f(void) only.

编译器拒绝实现 Derived::f(int) 因为它想调用 f(int) 但我没有提供参数因为我想调用 f(void).为什么编译器会拒绝它?为什么添加行 virtual int f(void) = 0; 可以解决我的问题?

The compiler rejects the implementation Derived::f(int) because it wants to call f(int) but I provided no arguments because I want to call f(void). Why does the compiler reject it? Why does adding the line virtual int f(void) = 0; fix my problem?

class Base
{
public:
  explicit Base(void) {}
  virtual ~Base(void) {}

  virtual int f(void) = 0;
  virtual int f(int i) = 0;
};

class Derived : public Base
{
public:
  // provide implementation for f(int) which uses f(void). Does not compile.
  virtual int f(int i) {puts("Derived::f(int)"); return f();}
  // code only compiles by adding the following line.
  virtual int f(void) = 0;
};

class Derived2 : public Derived
{
public:
  // overwrite only f(void). f(int) is implemented by Derived.
  virtual int f(void) {puts("Derived2::f(void)"); return 4;}
};

int main(void)
{
  Base * p = new Derived2();
  int i0 = p->f();  // outputs Derived2::f(void) and returns 4
  int i1 = p->f(1); // outputs "Derived::f(int) Derived2::f(void)" and return 4
  delete p;
  return 0;
}

推荐答案

Derived::f 隐藏 Base::fs.给定Derived::f(int)的body中的return f();,在f这个名字>派生,然后名称查找停止.Base 中的名称将无法找到并参与重载解析.

Derived::f hides Base::fs. Given return f(); in the body of Derived::f(int), the name f is found in the scope of Derived, then name lookup stops. The names in Base won't be found and participate in overload resolution.

名称查找如下所述检查范围,直到找到至少一个任何类型的声明,此时查找停止并且不再检查范围.

name lookup examines the scopes as described below, until it finds at least one declaration of any kind, at which time the lookup stops and no further scopes are examined.

您可以添加using Base::f;,将Base中的名称引入Derived的范围.

You can add using Base::f; to introduce the name from Base into the scope of Derived.

class Derived : public Base
{
public:
  using Base::f;

  // provide implementation for f(int) which uses f(void).
  virtual int f(int i) {puts("Derived::f(int)"); return f();}
};

这篇关于为什么我需要重新声明重载的虚函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Polymorphic copy-constructor with type conversion(具有类型转换的多态复制构造函数)
Cast to a Child(铸造给孩子)
How to use virtual functions to achieve a polymorphic behavior in C++?(如何使用虚函数在 C++ 中实现多态行为?)
Polymorphism is not working with function return values of same data type (Base and Inherited class)(多态性不适用于相同数据类型(基类和继承类)的函数返回值)
Size of class with virtual function adds extra 4 bytes(具有虚函数的类的大小增加了额外的 4 个字节)
Right design pattern to deal with polymorphic collections of objects(正确的设计模式来处理对象的多态集合)