这与函数重载有什么关系? [英] What does that have to do with function overloading?

查看:143
本文介绍了这与函数重载有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上是来自项目21中给出的示例的副本。覆盖虚拟函数 in Herb Sutter's book Exceptional C ++

This is basically a copy from the example given in Item 21. Overriding Virtual Functions in Herb Sutter's book Exceptional C++.

#include <iostream>
#include <complex>
using namespace std;
class Base
{
public:
    virtual void f(int);
    virtual void f(double);
    virtual ~Base() {};
};

void Base::f(int) { cout << "Base::f(int)" << endl; }
void Base::f( double ) { cout << "Base::f(double)" << endl; }

class Derived: public Base {
public:
    void f(complex<double>);
};

void Derived::f(complex<double>) { cout << "Derived::f(complex)" << endl; }

int main()
{
    Base* pb = new Derived;
    pb->f(1.0);
    delete pb;
}

代码打印 Base :: f 我没有问题。但我不明白作者在第122页顶部给出的解释(重点是我的):

The code prints Base::f(double) and I have no problems with that. But I couldn't understand the explanation given by the author on the top of page 122 (emphasis is mine):


有趣的是, Base * pb指向一个Derived
对象,这会调用Base :: f(double),,因为重载分辨率是
在静态类型(这里是Base)上完成,而不是动态类型(
Derived)

我的理解是调用 pb- > f(1.0)是一个虚拟调用, Base :: f(double) f (双)派生。这与函数重载有什么关系?

My understanding is that the call pb->f(1.0) is a virtual call and Base::f(double) is the final overrider for f(double) in Derived. What does that have to do with function overloading?

推荐答案

这里的微妙部分是虚拟方法是一种机制, 。

The delicate part here is that virtual methods are a mechanism to dispatch the function call, while overloading is a feature that affects the resolution of the call.

也就是说,对于任何一个呼叫编译器需要找出应该调用哪个方法(解析它);然后在逻辑上不同的操作中,它需要生成调用该方法的正确实现的代码(dispatch it)。

That is, for any call the compiler needs to figure out which method should be called (resolve it); afterwards, and in a logically distinct operation, it needs to generate code that calls the correct implementation of that method (dispatch it).

Base 和 Derived 我们可以很容易地推理出如果 f(double) Base * 上调用,则调用应优先于基本实现分派到任何派生覆盖(如果适用)。但是回答的问题完全不同于

From the definitions of Base and Derived given above we can easily reason that if f(double) is called on a Base* then the call should be dispatched to any derived override (if applicable) in preference to the base implementation. But answering that is answering a question totally different than


当源说 pb-> f ,应该使用名为 f
的哪个方法来解析方法调用?

When the source says pb->f(1.0), which of the methods named f should be used to resolve the method call?

As Sutter解释说,规范说在解析调用时,编译器将查看在 f pb 指向的静态类型;在这种情况下,静态类型是 Base * ,因此在 Derived 上声明的重载(不覆盖!在所有。但是,如果调用解析的方法是 virtual ,那么 Derived 上提供的可能实现将按预期使用

As Sutter explains, the spec says that when resolving the call the compiler will look at the methods named f declared on the static type pointed to by pb; in this case, the static type is Base* so overloads (not overrides!) declared on Derived will not be considered at all. However, if the method that the call resolves to is virtual then the possible implementation provided on Derived will be used as expected.

这篇关于这与函数重载有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆