它们是一样的。
当您在 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;
}
运行时,上面的程序打印 123,而不是 3。到目前为止一切顺利。
多次分派是一种语言或运行时分派“this”指针的类型和方法参数的类型的能力。考虑(暂时坚持使用 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;
}
如果 C++ 有多次调度,程序将打印出“Called Derived::Foo with a Dervied*”。 (遗憾的是,C++ 没有多重调度,因此程序会打印出“Called Derived::Foo with a Base*”。)
Double-dispatch 是 Multiple-dispatch 的一种特殊情况,通常更容易模拟,但作为一种语言功能并不十分常见。大多数语言要么进行单次调度,要么进行多次调度。