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

  • Home
  • SEARCH
  • 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 8387823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:19:11+00:00 2026-06-09T18:19:11+00:00

In a non-boost project, I have a class which uses a timer based on

  • 0

In a non-boost project, I have a class which uses a timer based on a certain user action (button pressed/released). I want this class generic, so it takes callbacks for user defined actions.

// TimerClass.h
typedef void (*timerCallback)(void);
...
Class TimerClass : public CButton {
public:
    void setCallbackShortTime(timerCallback clickFn) { shortCallback = clickFn;} ;
    void setCallbackLongTime(timerCallback clickFn) { longCallback = clickFn;} ;
private:
    timerCallback shortCallback, longCallback;
}


// CMyDlg.h
class CMyDlg : public CDialog
{
public:
    void openLiveViewWindow();
    void openAdminPanelWindow();
    TimerClass _buttonSettings;
}

// CMyDlg.cpp
...
_buttonSettings.setCallbackShortTime(&openLiveViewWindow);
...

Now, from another class (DialogClass) I can use the TimerClass but I cannot pass function pointers to the callback functions. These functions are not static. The compiler ends up complaining:

error C2276: '&' : illegal operation on bound member function expression

Some research on this pointed out to std::function() and std::bind() but I’m not familiar with these and would appreciate some pointers on how to resolve this.

RESOLUTION: For anyone interested, here are the bricks of the final solution

// TimedButton.h
#include <functional>
// define timer callback prototype
typedef std::function<void()> timerCallback;
...
class TimedButton : public CButton
{
public:
    TimedButton();
    ...
    void setCallbackShortTime(timerCallback clickFn) { _shortCallback = clickFn;} ;
    void setCallbackLongTime(timerCallback clickFn) { _longCallback = clickFn;} ;
private:
    timerCallback _shortCallback;
    timerCallback _longCallback;
}

// TimedButton.cpp
...
(_longCallback)();  // call long duration fn
...
(_shortCallback)();     // call short duration fn

// in MyDlg.cpp
#include <functional>
...
_buttonSettings.setCallbackShortTime(std::bind(&CMyDlg::openLiveViewWindow, this));
_buttonSettings.setCallbackLongTime(std::bind(&CMyDlg::openAdminPanelWindow, this));
  • 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-09T18:19:13+00:00Added an answer on June 9, 2026 at 6:19 pm

    std::function is a polymorphic function object, which can wrap up any type of callable object with a particular signature. In your case, you want it to take no arguments and return no value, so you can define:

    typedef std::function<void()> timerCallback;
    

    std::bind allows you to adapt a callable object to one of a different signature, by binding arguments to parameters. In your case, you want to adapt a member function by binding it to a particular object to invoke it on:

    _buttonSettings.setCallbackShortTime(std::bind(&CMyDlg::openLiveViewWindow, this));
    

    Note that these were introduced in 2011, so older compilers won’t support them. In that case, you could either use the very similar Boost or TR1 libraries, or make your own callable class containing a pointer-to-member-function and a pointer/reference to the object you want to invoke it on.

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

Sidebar

Related Questions

Custom non-fatal exception derive from which class in asp.net 3.5?
I have a non ode function that calculates based on the previous time step
From http://www.boost.org/community/implementation_variations.html ... coding differences such as changing a class from virtual to non-virtual
I'm doing a project for a class which focuses on storing a huge matrix
I have a section of code that uses a Boost library that results in
I've an unmanaged C++ project in Visual Studio 2010. It uses boost, glut and
Boost is meant to be the standard non-standard C++ library that every C++ user
In a C++ project that uses smart pointers, such as boost::shared_ptr , what is
So I have a boost::multi_index_container with multiple non-unique indexes. I would like to find
I'm working on a project which uses Twisted to provide a high performance UDP

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.