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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:00:53+00:00 2026-05-26T02:00:53+00:00

I’m looking for some guidance in terms of best practices when dealing with custom

  • 0

I’m looking for some guidance in terms of best practices when dealing with custom callbacks or function pointers.

I have two main use cases right now.

  1. I have a blocking loop inside my class that loops forever. I want an external user to be able to specify a function that gets called on each iteration of that loop.

Ex.

myClass.setLoopFunction(doStuff); 

Where doStuff is a function located elsewhere that will get called on each iteration of the loop.

  1. I get back an int from a function that can correspond to multiple values. I want to dynamically map functions to those values so I can just do a lookup on the int I get back and then call the corresponding function.

Ex.

myFunctionMap[passedInt]();

Where the int acts as a key to lookup the correct function to call.

I know my syntax here is probably off and I will need to use a std::map and be passing pointers etc, but any help, guidance or pitfalls would be appreciated.

Thanks!

EDIT:

I have the following declared for now as a public variable:

class Window {

public:
//The processing function can be from any class and takes in no arguments and returns void
template<class T>
std::function<void(T*, void)> processingFunction;

};

The functionality I want to have is that any class can pass in a function that takes in 0 arguments and returns void and I will set that as my processing function. In the while loop, I will execute that processingFunction and it will call the member function on that original class.

while(true) {
    if (exitCondition == false) {
        //Execute processing function
        processingFunction();
    }
    else {
        break;
    }
}

I feel like I’m completely missing something with templates, std::function and/or std::bind. Then again it’s been a good four hours of scouring the internet so maybe I just need to sleep on 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-26T02:00:54+00:00Added an answer on May 26, 2026 at 2:00 am

    I would recommend you use std::function< void() > instead of pointers. Note that this is a TR1 addition to the standard library, you can use Boost implementation of it to work on older implementations. The benefit of it is that it takes any kind of function that takes no arguments and returns something compatible with void (that is: returns anything). You can use it in conjuntion with std::bind to get even more flexibility.

    That said, you would still need an std::map< int, std::function< void() > > (or an ordinary std::vector if your indices are sequential) to map from indices to functions.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.