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 6584689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:34:05+00:00 2026-05-25T16:34:05+00:00

Hello in one of my current projects I want to implement an InputMap. So

  • 0

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 ) = 0;
};
}

and an InputMap

//InputMap.h
namespace INPUT {
class InputMap {
public:
    InputMap();
    virtual void HandleInput( INPUT::Input & anInput ) {
    }
    virtual ~InputMap();
};
}

so far so good – no functionality here. Now I derive my first real Input from my abstract input class:

//StringInput.h
#include "Input.h"
#include "InputMap.h"
#include <string>

class StringInput : INPUT::Input {
public:
    StringInput();
    virtual ~StringInput();
    void Dispatch(INPUT::InputMap * pMap)
    {
        pMap->HandleInput( *this );
    }
    void SetMessage(std::string message);
    std::string GetMessage() const;
private:
     std::string m_message;
};

and here is my derived InputMap

//MyInputMap.h
#include "InputMap.h"
#include "StringInput.h"

class MyInputMap: public INPUT::InputMap {
public:
    MyInputMap();
    void HandleInput( StringInput & anInput );
    void HandleInput( INPUT::Input & anInput );
    virtual ~MyInputMap();
};

and here is the test:

//main.cpp
MyInputMap map;
StringInput input;
input.SetMessage("Test");
input.Dispatch(&map);

of course I expect that input.Dispatch(&map) invokes map.HandleInput(StringInput input), but unfortunately the default handler is always invoked. Did I program this pattern wrong? Thanks guys, I have been staring my code forever, but I don’t see it.

  • 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-25T16:34:06+00:00Added an answer on May 25, 2026 at 4:34 pm

    You should read about the Visitor pattern.

    Basically, the issue is that virtual function are statically bound (ironic), so the solution is to declare all HandleInput (for every single type of Input) in InputMap.

    class InputMap {
    public:
        InputMap();
        virtual void HandleInput(StringInput&) = 0;
        virtual void HandleInput(IntInput&) = 0;
        virtual ~InputMap();
    };
    

    Note: the convention is to use pure virtual methods, so that no derived class forgets from overriding one.

    Of course, this causes an issue of dependencies. Fortunately, you can forward declare the “real” input types in the header containing InputMap.

    There are more complicated variations (search for Acyclic Visitor), but you should not need it right now 🙂

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

Sidebar

Related Questions

hello i have created one windows application in c# .net and its working fine
I have one requirement in C. char abc[]=hello; char hello[]=world; Using abc whether we
I have three button named(titled) hello, nothing, heaven and one label (IBOutlet UIlabel lab).
Hello i have a quick question. I get 2 flash messages, the devise one
Hallo, How can I calculate a current time moment + exactly one week? The
Can any one explain why the output of this code is only 'hello' and
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc
I just started down the Assembly road, and one of the first Hello, World!
I have scripts that have one-liners or sort scripts from other languages within them.
for Internet Explorer, I have the current code to select a range in an

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.