多种调度和多种方法

peo*_*oro 0 oop multiple-dispatch double-dispatch multimethod

它们是什么,它们之间有什么不同?

许多消息来源,如维基百科,声称他们是同样的事情,但也有人明确表示,相反,像SBI这个问题:

首先:"访问者模式是一种模拟C++中双重调度的方法." 这是,呃,不完全正确.实际上,双调度是多调度的一种形式,这是一种在C++中模拟(缺失)多方法的方法.

Joh*_*oty 5

他们是一样的.

在C++中调用虚方法时,实际运行的方法基于调用它们的方法的对象的运行时类型.这称为"单一调度",因为它取决于单个参数的类型(在这种情况下,隐含的"this"参数).因此,例如,以下内容:

class Base {
  public:
    virtual int Foo() { return 3; }
}

class Derived : public Base {
  public:
    virtual int Foo() { return 123; }
}

int main(int argc, char *argv[]) {
  Base* base = new Derived;
  cout << "The result is " << base->Foo();
  delete base;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

运行时,上面的程序打印123,而不是3.到目前为止这么好.

多派遣是一种语言或运行时的调度上"这个"指针的两种类型的能力以及的参数传递给方法的类型.考虑(目前坚持使用C++语法):

class Derived;

class Base {
  public:
    virtual int Foo(Base *b) { cout << "Called Base::Foo with a Base*"; }
    virtual int Foo(Derived *d) { cout << "Called Base::Foo with a Derived*"; }
}

class Derived : public Base {
  public:
    virtual int Foo(Base *b) { cout << "Called Derived::Foo with a Base*"; }
    virtual int Foo(Derived *d) { cout << "Called Derived::Foo with a Derived*"; }
}

int main(int argc, char *argv[]) {
  Base* base = new Derived;
  Base* arg = new Derived;

  base->Foo(arg);

  delete base;
  delete arg;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果C++有多个调度,程序将打印出"使用Dervied*调用Derived :: Foo".(遗憾的是,C++没有多个调度,因此程序打印出"Called Derived :: Foo with a Base*".)

双调度是多调度的一种特殊情况,通常更容易模拟,但作为语言功能并不常见.大多数语言都是单发或多发.