In software engineering, double dispatch is a special form of multiple dispatch and a mechanism that dispatches a function call to different concrete functions depending on the runtime types of two objects involved in the call. In most OO systems, the concrete function that is called from a function call in the code depends on the dynamic type of a single object and therefore they are known as single dispatch calls, or simply virtual function calls.
Questions tagged [double-dispatch]
97 questions
2
votes
1 answer
What exactly happened that we need double dispatch/visitor in c++
I understand the implementation of the solution and the double dispatch/visitor pattern, however I don't understand what happens at compile time and run-time that we need this pattern.
For example this code:
#include
class A…

JayZ
- 244
- 1
- 7
2
votes
1 answer
Double dispatch without knowing the full hierarchy
I would like to implement the following thing in C++:
I would like to have a bunch of child classes of a single class with the ability to call a function that takes a pair of objects of any of these types. There is supposed to be a generic…

lytenyn
- 819
- 5
- 21
2
votes
3 answers
Double dispatch produces 'hides virtual function' warnings, why?
I would like to implement interactions between two objects whose types are derived from a common base class. There is a default interaction and specific things may happen once objects of the same type interact.
This is implemented using the…

lytenyn
- 819
- 5
- 21
2
votes
2 answers
Polymorphic binary function
I have three shape classes Circle, Square, ConvexPolygon, and I have the functions
bool ShapesIntersect(const Circle& s1, const Circle& s2);
bool ShapesIntersect(const Circle& s1, const Square& s2);
// ... and the other 7 combinations
I would like…

Andrea Allais
- 447
- 4
- 6
2
votes
3 answers
C++ Double Dispatch problems
This is part 2 to a problem I previously asked: Is it possible to have polymorphic member overloading in C++?
Using the Wiki example I created this example.
http://en.wikipedia.org/wiki/Double_dispatch
My problem is that the compiled code never…

Shawn Buckley
- 506
- 6
- 16
2
votes
4 answers
Check type visibility prior to dynamic double dispatch
Implementing double dispatch using dynamic:
public interface IDomainEvent {}
public class DomainEventDispatcher
{
private readonly List subscribers = new List();
public void Subscribe(Action subscriber)…

TrueWill
- 25,132
- 10
- 101
- 150
2
votes
2 answers
Observer pattern + Visitor pattern for message system
Recently I got into implementing a message dispatching system that uses the "Observer pattern": nothing special here. As I developed it I thought it would be nice to send "Message" objects from the "subject" that could be fundamentally different…

The Marlboro Man
- 971
- 7
- 22
2
votes
0 answers
Sparse double dispatch without dynamic -- is there a sane way to do it without O(n^2) code?
I have the following. It works. I want to do it in a way that avoids using dynamic and avoids having the code grow as O(n^2) in the number of types. The standard double dispatch solution I am aware of has O(n) code in each of the types A, B, C, D,…

William Jockusch
- 26,513
- 49
- 182
- 323
2
votes
1 answer
Select/use an implementation of an interface with a generic parameter based on type of another object
I'm working on a system for handling events:
public interface IEvent { ..}
public class CreateUserEvent : IEvent {...}
public class ChangeUserNameEvent : IEvent {...}
Each event has a specific handler
public interface IEventHandler where T :…

Roy T.
- 9,429
- 2
- 48
- 70
2
votes
1 answer
Double dispatch for dynamically typed operators in JavaScript
I want to formulate algebraic expressions in such a way that the underlying number types can be exchanged. If you want to, think about complex numbers, big integers, matrices and the likes. For this reason, I'd write either add(a, b) or a.add(b)…

MvG
- 57,380
- 22
- 148
- 276
2
votes
3 answers
How do I use double dispatch to analyze intersection of graphic primitives?
I am analyzing the interaction of graphics primitives (rect, line, circle, etc.) and computing the overlap, relative orientation, merging, etc. This is quoted as a prime example of Double Dispatch (e.g. Wikipedia)
Adaptive collision algorithms…

peter.murray.rust
- 37,407
- 44
- 153
- 217
2
votes
2 answers
Dynamic Double Dispatch without using dynamic keyword
I am trying to port this example of dynamic double dispatching to C#. I've got the example to work, but I feel like I've shortcut the DynamicDispatch method in the MessageBase class a bit by using Reflection to create the required handler and…

SwDevMan81
- 48,814
- 22
- 151
- 184
2
votes
2 answers
Double dispatch in Java example
I was reading the Wikipedia article on DD and jumped over to the "Double dispatch in Java and an example" link given at the end. The description of the following Serializable example seems rather confusing to me:
A a = new A();
ObjectOutputStream…

WXB13
- 1,046
- 3
- 11
- 17
1
vote
3 answers
Design to emulate Visitor without its drawbacks
I'm looking for a clean design to emulate Visitor functionality without the many drawbacks it has.
In Java, the traditional implementations (as the described in GoF) resort to double dispatch to get rid of if-elses. To solve this, I've seen some…

Mister Smith
- 27,417
- 21
- 110
- 193
1
vote
2 answers
Double dispatching fails for an InputMap in C++ - codes reduced to simplicity
Hello in one of my current projects I want to implement an InputMap. So I have an abstract input
//Input.h
namespace INPUT {
class InputMap;
class Input {
public:
Input();
virtual ~Input();
virtual void Dispatch( InputMap * pMap ) =…

chris.schuette
- 307
- 2
- 11