Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6051761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:51:13+00:00 2026-05-23T07:51:13+00:00

I want to implement double dispatch. Say I have a class like so: class

  • 0

I want to implement double dispatch.
Say I have a class like so:

class Entity
{
   public:
   virtual void eventHandler(Message* msg, Entity* entity);
}

Then eventually I create derived entities. Say I eventually create several different types which derive from Entity. Do I need to forward declare them all in Entity.hpp then make methods for each one? What if my class names change, it seems to add a maintenance overhead?

The other thing I was wondering is if I need to re implement each one in each class.

Say Player needs to respond to the event handler of an Item. Does that mean I need to provide empty implementations for all the other ones?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:51:13+00:00Added an answer on May 23, 2026 at 7:51 am

    In double-dispatch you have two class hierarchies, A and B. A has a bunch of overloaded methods that take different versions of B and B has a single method that takes A. Each of these methods need one argument. You need to declare all different versions in the A base class and implement all of them in A derived classes. You also need to reimplement the B method in each B derived class. Yes this means A knows about each one of the B versions in advance.

    Let’s say you have 3 concrete messages, a KeyboardMessage, a MouseMessage ans a ScreenMessage. You also have a growing family of Entities, at this time it consists of a Widget and a Gadget. In a C++-like pseudocode

    class Message { virtual void dispatch(Entity* e) = 0 }
    class KeyboardMessage : Message { void dispatch(Entity* e) { e->process(this); }}
    class MouseMessage : Message { void dispatch(Entity* e) { e->process(this); }}
    class ScreenMessage : Message { void dispatch(Entity* e) { e->process(this); }}
    
    class Entity
      {
         virtual void process (KeyboardMessage* m) {... default impl...}
         virtual void process (MouseMessage* m) {... default impl...}
         virtual void process (ScreenMessage* m) {... default impl...}
      }
    
     class Widget : Entity
      {
         void process (KeyboardMessage* m) {... impl...}
         void process (MouseMessage* m) {... impl...}
         void process (ScreenMessage* m) {... impl...}
      }
    
    class Gadget : Entity
      {
         void process (KeyboardMessage* m) {... impl...}
         void process (MouseMessage* m) {... impl...}
         void process (ScreenMessage* m) {... impl...}
      }
    

    Now you can add other concrete entities with ease. To add another message, you will have to add a method to Entity, possibly reimplement it in some derived classes, but recompile all derived (since the base has changed).

    See, no casts here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some code like this: class Base { public: virtual int Foo(int)
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement in Java a class for handling graph data structures. I
I want to implement an ISAPI filter like feature using HttpModule in IIS7 running
I have a char pointer returned from a function, and i want to double
I have variables which are of double type I want them to be float
I have a created a custom class that extends from MKMapView and i want
I am very new to Scala. I want to implement a generic matrix class
Consider the following setup: I am given an interface template<class T> void FooClass<T>::foo(boost::function<double (int)>
I want to implement a slider-like component like the one at the bottom of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.