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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:08:44+00:00 2026-06-03T22:08:44+00:00

I have a functioning C++ callback function, triggered by a user ‘mouse down’ event.

  • 0

I have a functioning C++ callback function, triggered by a user ‘mouse down’ event. (The IDE is VS2010.)

With each call, I’d like to increment a simple count variable that is local to the callback’s scope. Simply put, what is the ‘best practices’ way to do this?

Thanks in advance for any opinions or directives.

  • 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-03T22:08:46+00:00Added an answer on June 3, 2026 at 10:08 pm

    Replace your callback function with a functor – they can store state. An example functor:

    #include <iostream>
    #include <memory>
    
    class Functor
    {
    private:
        std::shared_ptr<int> m_count;
    
    public:
        Functor()
        :    m_count(new int(0))
        {}
    
        void operator()()
        {
            ++(*m_count);
            // do other stuff...
        }
    
        int count() const
        {
            return *m_count;
        }
    };
    
    template <typename F>
    void f(F callback)
    {
        // do stuff
        callback();
        // do other stuff
    }
    
    int main()
    {
        Functor callback;
        f(callback);
        f(callback);
        std::cout << callback.count();    // prints 2
        return 0;
    }
    

    Note the use of a shared_ptr inside the functor – this is because f has a local copy of the functor (note the pass-by-value) and you want that copy to share its int with the functor to which you have access. Note also that f has to take its argument by value, since you want to support all callables, and not just functors.

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

Sidebar

Related Questions

I have a functioning Silverlight 4 application (VS2010, SL4, WCF RIA, hosted on my
I have 5 MySQL InnoDB tables: Test,InputInvoice,InputLine,OutputInvoice,OutputLine and each is mapped and functioning in
I have a fully functioning website with user accounts/profiles and admin area. I would
I have a web.config of a functioning MOSS server with our custom settings and
I have a CSS dropdown menu that is functioning how I want it to
I have functioning RMI client-server system. The server's host information is known to the
Here's the idea. I'm coding a chat bar, I have everything functioning. But my
I have a fully functioning program that worked. However it had two dialogs that
I have my Sharepoint Server 2010 set-up and everything appears to be functioning correctly.
I have an otherwise functioning rails project where I'm trying to update some form

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.