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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:13:22+00:00 2026-05-21T12:13:22+00:00

To overcome the impossibility of giving C library a callback to C++ member function,

  • 0

To overcome the impossibility of giving C library a callback to C++ member function, wanted to implement something like this:

SomeObject* findSomeObjectByHandlePointer(datahandle *dh) { }..

by using a map, which contains addresses of *datahandle as an index, and addresses of *SomeObject’s as value.

When SomeObject is created, it produces a group of datahandle’s, which are unique for the object. Then, it passes a pointer to *datahandle and static callback function to C library, then C library calls back and returns a pointer to datahandle, that in turn can be associated back with a SomeObject.

Which types can you recommend for storing pointer values in a map besides safe but slow <string, SomeObject*>?


This answer tells me to avoid using auto_ptr too.

  • 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-21T12:13:22+00:00Added an answer on May 21, 2026 at 12:13 pm

    Normally, C-like callbacks take a void* user_data parameter, which allows you to pass in anything you want:

    void c_func(void (*fptr)(void*), void* user_data){
      // do some stuff
      fptr(user_data);
    }
    

    Now, simply make the following static member function:

    class A{
    public:
      static void c_callback(void* my_data){
        A* my_this = static_cast<A*>(my_data);
        // do stuff with my_this
      }
    };
    

    Edit: According to @Martin’s comment, you may get unlucky with a static member function. Better use an extern "C" function:
    extern “C” void c_callback(void* my_data){
    // same as static method
    }
    And pass that + your A instance to that c_func:

    int main(){
      A a;
      c_func(&A::c_callback,&a);
    }
    

    Or if that A instance needs to outlive the current scope, you need to somehow save the heap-allocated pointer somewhere and delete it manually later on. A shared_ptr or the likes won’t work here, sadly. 🙁


    On your problem of storing pointer in a map, that’s not a problem at all, see this little example on Ideone.

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

Sidebar

Related Questions

I tried everything but I couldn't overcome this problem. I have a table-valued function.
Here is my code: function myChmod($path, $permission, $log) { //this is to overcome umask
How can I load an external JavaScript file using a bookmarklet? This would overcome
I know this is silly but I can't overcome my curiosity. Is it possible
I want to know if there is any pattern that can overcome this problem:
I have the following function which works great, i used JSONP to overcome cross-domain,
Why this DataGridView control has a gray strips? How can I overcome this problem?
I've seen many articles about how to overcome this matter, all related to CTP4,
Im new to PHP and I was wondering how I can overcome this seemingly
I've actually been working on this for the past few hours... I've overcome the

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.