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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:58:06+00:00 2026-06-11T12:58:06+00:00

I have one application in which following task are to be done 1.) UI

  • 0

I have one application in which following task are to be done

1.) UI application will send command code (integer value).
2.) DLL interface(in c++) will get that integer value and execute corresponding command function.

commands name and command code are maintained as

#define PING 50

there will be 500 commands and applying SWITCH CASE will not sound good. so i decided to implement function pointer in my code as below

   #include "stdafx.h"

    #include<iostream>
    #define PING 20

    using namespace std;
    //extern const int PING = 10; 
    void ping()
    { 
                    cout<<"ping command executed";
    }


    void get_status(void)
    {


    cout<<"Get_status called"<<endl;

    }

    class ToDoCommands
    {
            public:
                void getCommand( void (*CommandToCall)() );                         
    };



    void ToDoCommands::getCommand( void (*CommandToCall)())
    {


        void (*CommandToCall1)();

        CommandToCall1  = CommandToCall;

        CommandToCall1();

    }

    int main()
    {
            int code;
            ToDoCommands obj;
            cout<<"enter command code";
            cin>>code;  // if UI send 50 then Ping function get executed as #define PING 50

            obj.getCommand(ping);   // here m passing ping manually..
            //obj.getCommand(get_status);

                return 0;
    }

how can i pass command name corresponding to command code in

obj.getCommand(ping); 
  • 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-11T12:58:07+00:00Added an answer on June 11, 2026 at 12:58 pm

    You are almost there: make a std::map of std::string to function pointer, initialize it with data pairing a string name to a corresponding function pointer, and then use that map at runtime to pick the correct pointer based on the string parameter passed in.

    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    void ping() {
        cout << "ping" << endl;
    }
    void test() {
        cout << "test" << endl;
    }
    int main() {
        map<string,void(*)()> m;
        m["ping"] = ping;
        m["test"] = test;
        // I am using hard-coded constants below.
        // In your case, strings will come from command line args
        m["test"]();
        m["ping"]();
        return 0;
    }
    

    Link to a demo with std::map.

    Here is how you can do it without a map (it will be slower because of the linear search, but you can fix it by ordering names alphabetically and using binary search).

    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    void ping() {
        cout << "ping" << endl;
    }
    void test() {
        cout << "test" << endl;
    }
    typedef void (*fptr_t)();
    int main() {
        const fptr_t fptrs[] = {test, ping};
        const char *names[] = {"test", "ping"};
        const char *fname = "test";
        for (int i = 0 ; i != 2 ; i++) {
            if (!strcmp(fname, names[i])) {
                fptrs[i]();
                break;
            }
        }
        return 0;
    }
    

    Link to a demo with arrays.

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

Sidebar

Related Questions

I have the following patterns and grouping application which I am refactoring in one.
I am developing one application which need the following functionalities I have webserver. I
I want to accomplish the following task. I have an web application through which
i have one application which is build using VB.NET now i required to add
I have one web application which I have been working for some time.I am
I have one desktop application which was implemented in C#. This app uploads file
The problem: we have one application that has a portion which is used by
My application is viewbased application in which I have one button. On click of
I have an application which obtains data in JSON format from one of our
I have a Java application which requires certain software (one of them being Perl)

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.