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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:57:32+00:00 2026-05-22T23:57:32+00:00

I am building a program in c++ where the user can set a function

  • 0

I am building a program in c++ where the user can set a function to be called when user defined conditions are reached. I am only a little experienced with c++.

I know how to do this in python. You would simply define functions and put the names of said functions into a structure (I always used a dictionary). When you go to use the function, you would make a call similar to:

methods = { "foo" : foo, "bar" : bar } 
choice = input("foo or bar? ")
methods[choice]()

Any ideas on how to pull this off in c++ without having to hardcode everything?

  • 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-22T23:57:33+00:00Added an answer on May 22, 2026 at 11:57 pm

    Your Python code actually translates to C++ pretty much directly:

    # Python:
    # Create a dictionary mapping strings to functions
    methods = { "foo" : foo, "bar" : bar } 
    // C++:
    // create a map, mapping strings to functions (function pointers, specifically)
    std::map<std::string, void(*)()> methods; 
    methods["foo"] = foo;
    methods["bar"] = bar;
    
    # Python
    choice = input("foo or bar? ")
    // C++:
    std::string choice;
    std::cout << "foo or bar? ";
    std::cin >> choice;
    
    # Python:
    methods[choice]()
    // C++
    methods[choice]();
    

    Python’s dictionary is similar to C++’s map. They’re both associative containers, mapping a value from one type to a value of another (in our case, string to function).
    In C++, functions aren’t quite first-class citizens, so you can’t store a function in a map, but you can store a pointer to a function. Hence the map definition gets a bit hairy, because we have to specify that the value type is a “pointer to a function which takes no arguments and returns void”.

    On a side note, it is assumed that all your functions have the same signature. We can’t store both functions that return void and functions that return an int in the same map without some additional trickery.

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

Sidebar

Related Questions

I am building programming contest software. A user's program is received by our judging
I am building a C# program that unzips a file, and work on this
I am building a time registration program. Users can work on a project, and
How can i program this, the decimal shift needs to be programed that most
I'm currently building a program which adds to the current user's shell depending on
I have this old Fortran executable that can only be accessed through its GUI,
This program will have an infinite canvas (ie as long as the user scrolls,
I have downloaded a code when I am building the program, a message window
I'm building my c++ program with cmake on a Mac. The compiler gives me
I am building a simple scheduling program for week long scheduling: Sunday - Saturday.

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.