Callback C++ Class method from C I am trying to callback C++ class methods from C. Now I have got something working, but am not sure if this is the best solution.
I am looking for feedback, as to if there is something wrong with this approach, and how could I make this better.
So, here goes my code:
This is the C++ class Foo. The class has the methods getValue() &amp; setValue(), which I would like to like to be called-back from the C code:
Contents of Foo.h:
#ifdef __cplusplus
class Foo
{
public:
    Foo();
    int getValue() const;
    void setValue(int value);

private:
    int value_;
};
#endif

// Free functions
int  c_wrapper_getValue(void *arg1);
void c_wrapper_setValue(int value, void *arg1);

Contents of the source file, Foo.cpp:
#include &quot;Foo.h&quot;

Foo::Foo() :
    value_{0}
{}

int Foo::getValue() const
{
    return value_;
}

void Foo::setValue(int value)
{
    value_ = value;
}

void c_wrapper_setValue(int value, void *arg1)
{
    Foo* foo_instance = static_cast&lt;Foo*&gt;(arg1);
    foo_instance-&gt;setValue(value);
}

int c_wrapper_getValue(void *arg1)
{
    Foo* foo_instance = static_cast&lt;Foo*&gt;(arg1);
    return foo_instance-&gt;getValue();
}

Here is the code for the C files, which in turn call the Foo class members. First, the header Test.h file:
#include &lt;stdio.h&gt;
#include &quot;Foo.h&quot;

#ifdef __cplusplus
extern &quot;C&quot; {
#endif

// Callback function pointers
typedef int  (*get_handler_t)(void* arg1);
typedef void (*set_handler_t)(int foo, void* arg1);

void register_handler(get_handler_t g, set_handler_t s, void* p_instance);

// These are the functions which call the callback functions
int call_cpp_get_function();
void call_cpp_set_function(int value);

#ifdef __cplusplus
}
#endif

And finally the Test.c file:
#include &quot;Test.h&quot;

static get_handler_t get_handler_ = NULL;   /**&lt; Getter function pointer of type int (*g)(void *). */
static set_handler_t set_handler_ = NULL;   /**&lt; Setter function pointer of type void (*s)(int,  void *). */
static void* foo_object_instance  = NULL;    /**&lt; Instance of Foo object. */

//! Registers the getter and setter function with the object of Class Foo
//! \param g Getter function pointer of type int (*g)(void *).
//! \param s Setter function pointer of type void (*s)(int,  void *).
//! \param p_instance Instance of Foo object.
void register_handler(
    int (*g)(void *),
    void (*s)(int,  void *),
    void* p_instance)
{
    get_handler_ = g;
    set_handler_ = s;
    foo_object_instance = p_instance;
}

//! Function calls the get function of class Foo
//! \return Value of value_ from class Foo
int call_cpp_get_function()
{
    return get_handler_(foo_object_instance);
}

//! Function calls the set function of class Foo
//! \param value Value to be set in class Foo
void call_cpp_set_function(int value)
{
    set_handler_(value, foo_object_instance);
}

Now I could create an object and call the functions present in my C file, which called back the C++ methods, like this:
Foo foo_object;
register_handler(&amp;c_wrapper_getValue, &amp;c_wrapper_setValue, static_cast&lt;void *&gt;(&amp;foo_object));
call_cpp_get_function();
call_cpp_set_function(35);

I do have some constraints in my case, for example I can't use STL, since the code is supposed to run on an embedded system. So, how bad is my code?
Encapsulate stuff that shouldn't be public
I'd rather make the callback functions private static functions of the c++ class This helps to encapsulate the nasty void*casts:
#ifdef __cplusplus
class Foo {
public:
    Foo();
    int getValue() const;
    void setValue(int value);

    void register_callbacks();

private:
    int value_;

    static int  cb_getValue(void *arg1);
    static void cb_wrapper_setValue(int value, void *arg1);
};
#endif

Implementation:
void Foo::register_callbacks() {
    register_handler(Foo::cb_getValue, Foo::cb_setValue, static_cast&lt;void *&gt;(this));
}

void Foo::cb_setValue(int value, void *arg1) {
    Foo* foo_instance = static_cast&lt;Foo*&gt;(arg1);
    foo_instance-&gt;setValue(value);
}

int Foo::cb_getValue(void *arg1) {
    Foo* foo_instance = static_cast&lt;Foo*&gt;(arg1);
    return foo_instance-&gt;getValue();
}

Usage:
Foo foo_object;
foo_object.register_callbacks();
call_cpp_get_function();
call_cpp_set_function(35);