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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:37:47+00:00 2026-06-11T11:37:47+00:00

Below is my code…. i had use an map here..instead of map i want

  • 0

Below is my code….

i had use an map here..instead of map i want to use 2-d array .
user will enter command code say 50 and corresponding function will be called that is registered like

to_do_commands.registerCommand(50,get_status);

full code below

#include "stdafx.h"
#include <assert.h> 
#include <iostream> 
#include <map> 
#include <string> 
 using namespace std;


void ping(void) 
{ 
  cout << "ping command executed\n"; 
} 

void get_status()
{

cout<<"get status executed";
}

class ToDoCommands 
{ 
  public: 
    typedef void (*FunctionPtr)(); 
    typedef int Code; 

    void registerCommand(Code code,FunctionPtr); 
    void callFunction(Code); 

  private: 
    map<Code,FunctionPtr> func_map; 
}; 

void ToDoCommands::registerCommand(Code code,FunctionPtr func_ptr) 
{ 
  func_map[code] = func_ptr; 
} 

void ToDoCommands::callFunction(Code code) 
{ 
  assert(func_map.find(code)!=func_map.end()); 
  func_map[code](); 
} 

int main(int argc,char **argv) 
{ 
  ToDoCommands to_do_commands; 

 // to_do_commands.registerFunction(50,ping);
  to_do_commands.registerCommand(10,ping);
  to_do_commands.registerCommand(50,get_status);
  to_do_commands.callFunction(50); 

  return 0; 
} 
  • 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-06-11T11:37:48+00:00Added an answer on June 11, 2026 at 11:37 am

    If you want to avoid any STL-dependence in you header file, you could use the PIMPL idiom and use anything you want in your implementation file (e.g. std::map).

    It could look like this:

    // ToDoCommands.h
    
    class ToDoCommands
    {
    public:
      typedef void(*FunctionPtr)();
      typedef int Code;
    
      ToDoCommands();
      ~ToDoCommands();
    
      void registerCommand(Code code, FunctionPtr fptr);
      void callFunction(Code code) const;
    protected:
    private:
      ToDoCommands(ToDoCommands const &); // do not copy
      ToDoCommands & operator=(ToDoCommands const &); // do not assign
    
      class ToDoCommandsImpl;
      ToDoCommandsImpl * m_pimpl;
    };
    
    // ToDoCommands.cpp
    
    #include "ToDoCommands.h"
    
    #include <map>
    
    class ToDoCommands::ToDoCommandsImpl
    {
    public:
      void registerCommand(Code code, FunctionPtr fptr)
      {
        m_map[code] = fptr;
      }
    
      void callFunction(Code code) const
      {
        std::map<Code, FunctionPtr>::const_iterator iter = m_map.find(code);
        iter->second();
      }
    
    protected:
    private:
      std::map<Code, FunctionPtr> m_map;
    };
    
    ToDoCommands::ToDoCommands()
    : m_pimpl(new ToDoCommandsImpl())
    { }
    
    ToDoCommands::~ToDoCommands()
    {
      delete m_pimpl;
    }
    
    void ToDoCommands::registerCommand(Code code, FunctionPtr fptr)
    {
      m_pimpl->registerCommand(code, fptr);
    }
    
    void ToDoCommands::callFunction(Code code) const
    {
      m_pimpl->callFunction(code);
    }
    

    I have disallowed copying and assignment of the class for simplicity, you can add those methods if you really need them.

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

Sidebar

Related Questions

Below code is working for list view, but i want to use listbox instead
HI: Below code is from RequestFactoryEditorDriver: /** * Returns a new array containing the
Below code gives this error message Specified method is not supported. But here is
Below code only showing the textfield, but I want to include textfield and textarea.
Below code truncate number after decimal NumberFormat blah= new DecimalFormat(#.##); But I want to
The below code will block all the keyboard shortcuts. Since i wanted to implement
below code can not run def map = [name:Test :: ( %2f %25 \$
Below code gives me an 'undefined' on the name and email. However the user
The below code throws an exception when executing this line (i.e. something.Add(name)). I want
The below code works if $a is an Array of Arrays, but I need

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.