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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:31:15+00:00 2026-06-08T06:31:15+00:00

First, some background: (Note: Though I’m in non-.NET Win32 land, this is really a

  • 0

First, some background:
(Note: Though I’m in non-.NET Win32 land, this is really a C++ question)

I’m using a 3rd party API which requires you to register a callback function in order to know when an async operation is complete. Gotta use the callback, no way around it.

A non-OOP implementation would be something like this:

void __stdcall MyCbFcn(int value)
{
   do something with 'value'...
}

API_RegisterCallback(MyCbFcn);

Pretty standard stuff.

BUT…
My code is OOP, with multiple instances rx’ing the callback, thus the callback needs to be routed to the object that registered it.

Knowing that folks do this, callbacks typically include a user var, something like:

void __stdcall MyCbFcn(int value, U32 user)
{
   do something with 'value'...
}

API_RegisterCallback(MyCbFcn, someUserValue);

and more specifically, when combined with OOP, this user arg allows you to get back into context:
(written inline for brevity):

class MyClass
{
public:
   MyClass()
   {
      API_RegisterCallback(MyClass::StaticCbFcn, (U32)this);
   }

private:
   static void __stdcall StaticCbFcn(int value, U32 user)
   {
      MyClass* pThis = (MyClass*)user;
      pThis->InstanceCbFcn(value);
   }
   void InstanceCbFcn(int value)
   {
      ... do some work in context ...
   }
}

BUT, my API doesn’t feature a user arg 🙁

So now my question:
How I can get back into context?

I’ve considered kinda sketchy things like defining a “pool” of 100 distinct callbacks and assigning them as objects are created, but that seems like a real hack.

An obvious solution … if I were in e.g. JavaScript 🙂 … would be to use an anonymous function, but AFAIK C++ doesn’t have anything like that.

Any ideas would be appreciated.

  • 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-08T06:31:17+00:00Added an answer on June 8, 2026 at 6:31 am

    “100 distinct callbacks” is really the only thing you can do, thus you use the function address as identifying parameter. It might help to implement the different functions as template with a constant parameter:

    template < unsinged N >
    void StaticCbFcn( int value )
    {
        map[ N ].InstanceCbFcn( value );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first question on stack overflow. Some quick background, this is not
First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
First some background, I've written an open source .NET library, named Duplicitiy (on github.com),
Hi and thanks for your attention. First some background on the question: I have
First, some background: I'm developing a web application using Python. All of my (text)
this is my first post here. First off, some background, I'm new to C#
Note first of all that this question is not tagged winforms or wpf or
Note: This might seem like a Super User question at first, but please read
First some background: I'm working on an application and I'm trying to follow MVVM
Some setup background first: I have a cronjob which runs a PHP file called

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.