double-dispatch

Double dispatch/multimethods in C++

扶醉桌前 提交于 2019-12-17 11:51:25
问题 I have a question on C++ double dispatch. In the code below, I want the results from the second set to match the results from the first set. I don't know the actual type (unless I try dynamic_cast) but I do know that the object inherited from the BaseClass type. What is the most efficient (performance-wise) way to accomplish this? After googling around for a while I found out about double dispatch and the loki multimethods. The problem I have with the Shape examples is that in my application,

C++ Double Dispatch for Equals()

喜夏-厌秋 提交于 2019-12-17 09:59:43
问题 Imagine I have abstract base class Shape , with derived classes Circle and Rectangle . class Shape {}; class Circle : public Shape {}; class Rectangle : public Shape {}; I need to determine if two shapes are equal, assuming I have two Shape* pointers. (This is because I have two instances of vector<Shape*> and I want to see if they have the same shapes.) The recommended way to do this is double dispatch. What I've come up with is this (greatly simplified here, so that shapes are equal to all

C++ Polymorphic Circular depenencies with double dispatch

送分小仙女□ 提交于 2019-12-13 17:40:29
问题 So, I'm having a big issue with circular dependency. My Square.h and Circle.h classes both inherit Shape.h, and use double dispatch in order to try and detect collision between the two. My classes are currently setup in the following sort of fashion Shape.h class Shape { public: virtual bool detectCollision(Shape* obj) = 0; virtual bool detectCollision(Square* obj) = 0; virtual bool detectCollision(Circle* obj) = 0; Square.h #include "shape.h" class Square : public Shape { public: bool

How to build double dispatch using extensions

半城伤御伤魂 提交于 2019-12-13 14:41:17
问题 I have a library that have a hierarchy of class as follow: class Base {} class A : Base {} class B : Base {} Now I wanted to do different thing depending on type of my object whether it is an A or a B. So I decide to go for and implement double dispatching to avoid checking type. class ClientOfLibrary { public DoStuff(Base anObject) { anObject.MakeMeDoStuff(this); } private void DoStuffForanA(A anA); private void DoStuffForaB(B aB); } Now the canonical way of implementing double dispatch is

Double dispatch in Java example

て烟熏妆下的殇ゞ 提交于 2019-12-11 01:47:44
问题 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 oos = new ObjectOutputStream(); oos.writeObject( a); Here's the description: In order to serialize A, ObjectOutputStream first looks to see if the method writeObject( ObjectOutputStream oos) exists. If it does, then it calls that method with itself

Visitor pattern where the visitors choose how to traverse

Deadly 提交于 2019-12-07 09:51:46
问题 As I understand it, in the typical specification of the Visitor pattern, it is the visited objects that decide how to traverse, and generally, they only support one traversal order. (See, e.g., here or here.) Is there a name for the same use of double-dispatch, but where the Visitors get to decide how to traverse the object-hierarchy? In my application, a very heterogeneous collection of document-model types is pushing towards visitors for, e.g., export operations. It seems rigid, though, to

Observer pattern + Visitor pattern for message system

巧了我就是萌 提交于 2019-12-06 04:30:31
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 from each other and could be read from the many "observers". These different messages took the form of different message classes (for example, think about the "User Logout message", "Screen mode toogle" and "Volume level changed", all of these need different information) and soon I found that the "observers" needed not to know about every different

Storing vector of std::shared_ptr<Foo> where Foo is a templated class

心不动则不痛 提交于 2019-12-06 00:37:08
I have a base class that I made a template because I want to vary the type it takes for several functions, but I want to derive from these templated base classes. I want to store a vector of these classes. My idea was to create a non-templated base class above everything in the hierarchy, and use double dispatching to figure out the type. Am I doing this the "right way"? Here's a code snippet of the scenario: class FooBase { public: virtual void Accept( Visitor &v ); }; template<class T> class Foo : public FooBase { public: virtual void DoThing( const T & ); virtual void Accept( Visitor &v) =

Visitor pattern where the visitors choose how to traverse

ε祈祈猫儿з 提交于 2019-12-05 17:38:38
As I understand it, in the typical specification of the Visitor pattern, it is the visited objects that decide how to traverse, and generally, they only support one traversal order. (See, e.g., here or here .) Is there a name for the same use of double-dispatch, but where the Visitors get to decide how to traverse the object-hierarchy? In my application, a very heterogeneous collection of document-model types is pushing towards visitors for, e.g., export operations. It seems rigid, though, to say that the various processors (visitors) should all traverse in, say, a breadth-first order. Some of

Double dispatch for collision handling with SpriteKit

跟風遠走 提交于 2019-12-05 02:34:30
问题 I'm using SpriteKit's collision detection. It has a callback that looks like this: - (void)didBeginContact:(SKPhysicsContact *)contact The contact object has two physics bodies: SKPhysicsBody *bodyA; SKPhysicsBody *bodyB; My game will have lots of objects, and of course I can test the categoryBitMask to find out what collided with what. But given that I intend to have many kinds (not more than 32 of course) and might dynamically introduce new types, what's the most elegant way to do dynamic