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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:50:13+00:00 2026-05-19T15:50:13+00:00

I have a C library function that expects a function pointer for callback, and

  • 0

I have a C library function that expects a function pointer for callback, and I want to pass in a C++ member function. The C++ function modifies a member variable, so I can’t use a static free function (as suggested in several similar posts). My attempt (shown below) fails with a compiler error.

This post comes closest to what I need:

Using a C++ class member function as a C callback function

How can I do this without static functions? Thanks!


test.h

#ifndef TEST_H_
#define TEST_H_

#ifdef __cplusplus
extern "C" {
#endif

typedef void (*handler_t)(int foo, void *bar);

void set_handler(handler_t h);

#ifdef __cplusplus
}
#endif

#endif

test.c

#include "test.h"
#include <stdlib.h>

static handler_t handler_ = NULL;
void set_handler(handler_t h) {
        handler_ = h;
}

void handle_event(int foo, void *bar) {
        if (handler_ != NULL) handler_(foo, bar);
}

test.cpp

#include "test.h"
#include <iostream>
using namespace std;

class Foo {
public:
        Foo() : ctr_(0) {};

        // handler needs to access non-static variable, so it can't be static
        void handler(int foo, void *bar) { ++ctr_;  }

private:
        int ctr_;
};

int main(int argc, char **argv) {
        // error: can't convert to "void (*)(int, void*)"
        set_handler(&Foo::handler);

        cout << "done" << endl;
        return 0;
}

GCC barf

$ gcc test.cpp test.c 
test.cpp: In function ‘int main(int, char**)’: 
test.cpp:18: error: cannot convert ‘void (Foo::*)(int, void*)’ to ‘void (*)(int, void*)’ for argument ‘1’ to ‘void set_handler(void (*)(int, void*))’
  • 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-19T15:50:14+00:00Added an answer on May 19, 2026 at 3:50 pm

    It is not possible, at least with that handler_t signature.

    While you can create a free function on your .cpp to wrap the member call, you need a pointer to the Foo instance:

    void my_wrap(int foo, void* bar) {
        Foo* some_foo_instance = ...;
        some_foo_instance->handler(foo, bar);
    }
    
    int main(int argc, char **argv) {
        set_handler(&my_wrap);
    }
    

    You need some void* to pass the Foo instance as a handler attribute:

    // Header
    typedef void (*handler_t)(int foo, void *bar, void* arg1);
    void set_handler(handler_t h, void* arg1);
    
    // Impl.
    void set_handler(handler_t h, void* arg1) {
            handler_ = h;
            handler_arg1_ = arg1;
    }
    
    // cpp
    void my_wrap(int foo, void* bar, void* arg1) {
        Foo* some_foo_instance = static_cast<Foo*>(arg1);
        some_foo_instance->handler(foo, bar);
    }
    
    // main
    int main(int argc, char **argv) {
        Foo some_concrete_instance;
        set_handler(&my_wrap, static_cast<void*>(&some_concrete_instance));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function in an external library that I cannot change with the
I have got a C function in a static library, let's call it A,
I have a function that takes a unsigned long* and needs to pass it
I have imported the kernel32 library. So, I have the createMutex function available but
Of course most languages have library functions for this, but suppose I want to
I have a C++ library and a C++ application trying to use functions and
I have a library A, that I develop. When I deploy it on a
I have a library consisting of approx 100 source files. I want one of
I have a library that I would like to license and distribute. I know
I am working on a quite large C library that doesn't have any tests

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.