passing functor as function pointer - c++

I'm trying to use a C library in a C++ app and have found my self in the following situation (I know my C, but I'm fairly new to C++). On the C side I have a collection of functions that takes a function pointer as their argument. On the C++ side I have objects with a functor which has the same signature as the function pointer needed by the C function. Is there any way to use the C++ functor as a function pointer to pass to the C function?

You cannot directly pass a pointer to a C++ functor object as a function pointer to C code
(or even to C++ code).
Additionally, to portably pass a callback to C code it needs to be at least declared
as an extern "C" non-member function.
At least, because some APIs require specific function call conventions and thus
additional declaration modifiers.
In many environments C and C++ have the same calling conventions and differ only
in name mangling, so any global function or static member will work.
But you still need to wrap the call to operator() in a normal function.
If your functor has no state (it is an object just to satisfy some formal
requirements etc):
class MyFunctor {
// no state
public:
MyFunctor();
int operator()(SomeType &param) const;
}
you can write a normal extern "C" function which creates the functor and executes its
operator().
extern "C" int MyFunctorInC(SomeType *param)
{
static MyFunctor my_functor;
return my_functor(*param);
}
If your functor has state, eg:
class MyFunctor {
// Some fields here;
public:
MyFunctor(/* some parameters to set state */);
int operator()(SomeType &param) const;
// + some methods to retrieve result.
}
and the C callback function takes some kind of user state parameter (usually void *):
void MyAlgorithmInC(SomeType *arr,
int (*fun)(SomeType *, void *),
void *user_state);
you can write a normal extern "C" function which casts its state parameter to
your functor object:
extern "C" int MyFunctorInC(SomeType *param, void *user_state)
{
MyFunctor *my_functor = (MyFunctor *)user_state;
return (*my_functor)(*param);
}
and use it like this:
MyFunctor my_functor(/* setup parameters */);
MyAlgorithmInC(input_data, MyFunctorInC, &my_functor);
Otherwise the only normal way to do it
(normal as in "without generating machine code at runtime" etc.)
is to use some static (global) or thread local storage to pass the functor
to an extern "C" function.
This limits what you can do with your code and is ugly but will work.

I found this "gem" using google. Apparently possible but I sure wouldn't recommend it. Direct link to example source code.

No, of course. The signature of your C function take an argument as function.
void f(void (*func)())
{
func(); // Only void f1(), void F2(), ....
}
All tricks with functors are used by template functions:
template<class Func>
void f (Func func)
{
func(); // Any functor
}

A C callback function written in C++ must be declared as an extern "C" function - so using a functor directly is out. You'll need to write some sort of wrapper function to use as that callback and have that wrapper call the functor. Of course, the callback protocol will need to have some way of passing context to the function so it can get to the functor, or the task becomes quite tricky. Most callback schemes have a way to pass context, but I've worked with some brain-dead ones that don't.
See this answer for some more details (and look in the comments for anecdotal evidence that the callback must be extern "C" and not just a static member function):
C++ Using Class Method as a Function Pointer Type

I don't think you can: operator() in a function object is really a member function, and C doesn't know anything about those.
What you should be able to use are free C++ functions, or static functions of classes.

GCC allows you to convert member function pointers to plain function pointers (the first argument of the function called by the plain function pointer then is this).
Check out the respective link in the manual.
This requires the -Wno-pmf-conversions flag in order to silence the respective warning for the decidedly non-standard feature. Very convenient for interfacing C style libraries with C++ style programming. When the member function pointer is a constant, this does not even need to generate any code at all: the API would use that argument order anyway.
If you already have a functor, flattening the functor in that manner will likely mean flattening its operator(), giving you a function that has to be called with a functor class pointer itself as its first argument. Which does not necessarily help all that much but at least has C linkage.
But at least when you are not going through functors this is helpful and provides a no-nonsense C linkage replacement for std::mem_fn from <functional>.

It depends if this is a static or instance method, if it is static then you can pass through the function as className::functionName, if it is an instance method it is fair more complicated, because you obviously need to tie to a certain instance but can't do it in the same way as you would with delegates in C# etc.
The best way I've found of doing this is to create a holding class which is instantiated with the instance of the object as well as the function pointer, the holding class can then invoke the function directly.

I would say no, because a C++ functor has an overloaded operator () which is a member function, and would thus require a member function pointer. This is a totally different data type than a normal C function pointer, since it cannot be invoked without an instance of the class. You'd need to pass a normal function or a static member function to the C library. Since an overloaded () operator can't be static, you can't do it. You'd need to pass the C-library a normal, non-member function or static member function, from which you can then invoke the C++ functor.

Hm, maybe you could write a free template function that wraps around your function-objects. If they all have the same signature, this should work. Like this (not tested):
template<class T>
int function_wrapper(int a, int b) {
T function_object_instance;
return funcion_object_instance( a, b );
}
This would do for all function that take two ints and return an int.

Many C APIs that take function pointer callbacks have a void* parameter for user state. If you've got one of those, you're in luck - you can use an exterm C function that treats the user data as some sort of reference or key to lookup the functor, then execute it.
Otherwise, no.

With C++11, you can use std::bind to solve the problem
#include <stdio.h>
#include <functional>
class aClass
{
public:
int i = 5;
aClass(int i_) : i(i_) {}
void operator()(int a, int b) {
i = i + 1;
printf ("%d + %d = %d i = %d\n", a, b, a + b + i, i);
}
};
void test (int a, int b)
{
printf ("%d - %d = %d\n", a, b, a - b);
}
template <class op>
void function1 (op function)
{
function (1, 1);
}
int
main (int argc, const char *argv[])
{
aClass a(1);
// function1 (a); // this is the wrong way of using it
using namespace std::placeholders;
std::function<void(int,int)> callback;
callback = std::bind(&aClass::operator(), &a, _1, _2);
function1 (callback);
function1 (callback);
function1 (test);
function1 (test);
return 0;
}

Related

C++ idiom when using a C library which expects a function pointer? [duplicate]

This question already has answers here:
In C++, is it safe/portable to use static member function pointer for C API callbacks?
(4 answers)
Closed 4 years ago.
I am using a C library in a C++ application. The C SDK has functions that take a callback function pointer as an argument. The signature of these functions is usually like:
typedef int (* Func) (type1 c, type2 d);
I have my code structured using classes in C++. However, I can't pass any member functions to this function as callback because it doesn't accept int (MyClass::*)(type1 c, type2 d) and only accepts int (*)(type1 c, type2 d).
I'm getting around this by defining all my callbacks as static in the various classes and then passing them to the C library which then works.
I am still new to C++ so I'm not sure if this is the right solution? The code works, but I'd love to hear if I'm doing this wrong.
On most platforms your code is ever likely to encounter, your solution is fine. However, strictly speaking, there can be a platform where the C calling convention and C++ calling convention differ. On this platform, your code would not work.
The 100% correct solution (as opposed to the 99.9% correct one which you use) is to define a function with C language linkage as the callback. If you need this function to have member access to your class, it can then call a static member function in your class:
class MyClass
{
static int secret;
public:
static void callback() { secret = 42; }
};
extern "C" void callback() { MyClass::callback(); }
Note that it is considered good practice for authors of callback registration points (that is, authors of the library which will call you back) to provide a way for the user to associated void* data with the callback, which will be passed in as they call you. If your library allows that, you can use this void *user_data to pass in a pointer to your C++ class. With such a nicely-designed callback library, it could look like this:
class MyClass
{
int secret;
public:
void setSecret() { secret = 42; }
};
extern "C" void callback(void *userData) { static_cast<MyClass*>(userData)->setSecret(); }
MyClass mc;
int main()
{
cLibrary_registerCallback(&callback, &mc);
}
The member functions always implicitly get 'this' as their first argument. The use of static functions often works as static functions don't need this implicit argument. Please see Angew's answer with extern "C" thing to get 100% portability.
To call a non-static member function as a call back, you need to define a non-member function, which takes an instance as an argument, and calls the member function on that instance. Such C API's that use a function pointer callback also allow registering a void pointer to user defined data that will be forwarded to the call back. Use this pointer to pass the instance.
Instead of a named function, it is typical to use a lambda, because such simple wrapper is typically not reusable and has no need for a name.

C++ Functor as an argument of an external fortran function [duplicate]

I'm trying to use a C library in a C++ app and have found my self in the following situation (I know my C, but I'm fairly new to C++). On the C side I have a collection of functions that takes a function pointer as their argument. On the C++ side I have objects with a functor which has the same signature as the function pointer needed by the C function. Is there any way to use the C++ functor as a function pointer to pass to the C function?
You cannot directly pass a pointer to a C++ functor object as a function pointer to C code
(or even to C++ code).
Additionally, to portably pass a callback to C code it needs to be at least declared
as an extern "C" non-member function.
At least, because some APIs require specific function call conventions and thus
additional declaration modifiers.
In many environments C and C++ have the same calling conventions and differ only
in name mangling, so any global function or static member will work.
But you still need to wrap the call to operator() in a normal function.
If your functor has no state (it is an object just to satisfy some formal
requirements etc):
class MyFunctor {
// no state
public:
MyFunctor();
int operator()(SomeType &param) const;
}
you can write a normal extern "C" function which creates the functor and executes its
operator().
extern "C" int MyFunctorInC(SomeType *param)
{
static MyFunctor my_functor;
return my_functor(*param);
}
If your functor has state, eg:
class MyFunctor {
// Some fields here;
public:
MyFunctor(/* some parameters to set state */);
int operator()(SomeType &param) const;
// + some methods to retrieve result.
}
and the C callback function takes some kind of user state parameter (usually void *):
void MyAlgorithmInC(SomeType *arr,
int (*fun)(SomeType *, void *),
void *user_state);
you can write a normal extern "C" function which casts its state parameter to
your functor object:
extern "C" int MyFunctorInC(SomeType *param, void *user_state)
{
MyFunctor *my_functor = (MyFunctor *)user_state;
return (*my_functor)(*param);
}
and use it like this:
MyFunctor my_functor(/* setup parameters */);
MyAlgorithmInC(input_data, MyFunctorInC, &my_functor);
Otherwise the only normal way to do it
(normal as in "without generating machine code at runtime" etc.)
is to use some static (global) or thread local storage to pass the functor
to an extern "C" function.
This limits what you can do with your code and is ugly but will work.
I found this "gem" using google. Apparently possible but I sure wouldn't recommend it. Direct link to example source code.
No, of course. The signature of your C function take an argument as function.
void f(void (*func)())
{
func(); // Only void f1(), void F2(), ....
}
All tricks with functors are used by template functions:
template<class Func>
void f (Func func)
{
func(); // Any functor
}
A C callback function written in C++ must be declared as an extern "C" function - so using a functor directly is out. You'll need to write some sort of wrapper function to use as that callback and have that wrapper call the functor. Of course, the callback protocol will need to have some way of passing context to the function so it can get to the functor, or the task becomes quite tricky. Most callback schemes have a way to pass context, but I've worked with some brain-dead ones that don't.
See this answer for some more details (and look in the comments for anecdotal evidence that the callback must be extern "C" and not just a static member function):
C++ Using Class Method as a Function Pointer Type
I don't think you can: operator() in a function object is really a member function, and C doesn't know anything about those.
What you should be able to use are free C++ functions, or static functions of classes.
GCC allows you to convert member function pointers to plain function pointers (the first argument of the function called by the plain function pointer then is this).
Check out the respective link in the manual.
This requires the -Wno-pmf-conversions flag in order to silence the respective warning for the decidedly non-standard feature. Very convenient for interfacing C style libraries with C++ style programming. When the member function pointer is a constant, this does not even need to generate any code at all: the API would use that argument order anyway.
If you already have a functor, flattening the functor in that manner will likely mean flattening its operator(), giving you a function that has to be called with a functor class pointer itself as its first argument. Which does not necessarily help all that much but at least has C linkage.
But at least when you are not going through functors this is helpful and provides a no-nonsense C linkage replacement for std::mem_fn from <functional>.
It depends if this is a static or instance method, if it is static then you can pass through the function as className::functionName, if it is an instance method it is fair more complicated, because you obviously need to tie to a certain instance but can't do it in the same way as you would with delegates in C# etc.
The best way I've found of doing this is to create a holding class which is instantiated with the instance of the object as well as the function pointer, the holding class can then invoke the function directly.
I would say no, because a C++ functor has an overloaded operator () which is a member function, and would thus require a member function pointer. This is a totally different data type than a normal C function pointer, since it cannot be invoked without an instance of the class. You'd need to pass a normal function or a static member function to the C library. Since an overloaded () operator can't be static, you can't do it. You'd need to pass the C-library a normal, non-member function or static member function, from which you can then invoke the C++ functor.
Hm, maybe you could write a free template function that wraps around your function-objects. If they all have the same signature, this should work. Like this (not tested):
template<class T>
int function_wrapper(int a, int b) {
T function_object_instance;
return funcion_object_instance( a, b );
}
This would do for all function that take two ints and return an int.
Many C APIs that take function pointer callbacks have a void* parameter for user state. If you've got one of those, you're in luck - you can use an exterm C function that treats the user data as some sort of reference or key to lookup the functor, then execute it.
Otherwise, no.
With C++11, you can use std::bind to solve the problem
#include <stdio.h>
#include <functional>
class aClass
{
public:
int i = 5;
aClass(int i_) : i(i_) {}
void operator()(int a, int b) {
i = i + 1;
printf ("%d + %d = %d i = %d\n", a, b, a + b + i, i);
}
};
void test (int a, int b)
{
printf ("%d - %d = %d\n", a, b, a - b);
}
template <class op>
void function1 (op function)
{
function (1, 1);
}
int
main (int argc, const char *argv[])
{
aClass a(1);
// function1 (a); // this is the wrong way of using it
using namespace std::placeholders;
std::function<void(int,int)> callback;
callback = std::bind(&aClass::operator(), &a, _1, _2);
function1 (callback);
function1 (callback);
function1 (test);
function1 (test);
return 0;
}

How to pass method of a class into another function in C++?

Say I have a class and a function
class A
{
int a;
int b;
int mul()
{
return a+b;
}
};
...
void func(int af, int bf, int (*fc)())
{
...
}
In the main function, the function is supposed to use the methods of class A
int main
{
A as;
...
func(as.a, as.b, as.mul);
}
However, I can't do that, the compiler keeps telling me that I'm passing
(int&, int&, 'unresolved overloaded function type')
into a function of candidate
(int, int, void(*)()).
Why is that, and how to pass method of a class into another function?
Oh, I think I should make the problem a little bit clearer. func(...) is actually a function from an algorithm I'm working on. And class A is a model that will use the algorithm to do the simulation. So I don't think I'll specifically use the instance of A in function B, but only pass A's methods and components in and work with them.
Update: Some have mentioned using static methods in class A. However, this is still a partial solution. Using static method mul() will force me to claim a and b both to be static. And if I have to use multiple instances of A, with method a, b different in each instance in my main function, using static variables will simply fail.
So, are there other suggestions on how to fix this without using static variables/methods? I remember in script languages such as python, passing methods of whatever type is basically not an issue. Why can't I do a similar thing in C++? Or are there workarounds in C++ that can help me do this?
First of all, mul is a member function of A, so you can't invoke it as if it was a global function or a static member function: it needs an object on which the function is invoked:
A::mul(); // ERROR!
A a;
a.mul(); // OK
This said, you could change the definition of func to accept a pointer-to-member-function:
void func(int af, int bf, void (A::*fc)())
// ^^^^^^^^^^^^^^^
However, this change alone won't make you progress too much, because you're still not passing the concrete instance of A on which the member function should be invoked. In other words, this would be illegal:
fc(); // ERROR!
To over come this limitation, you could pass a reference or a pointer to an instance of A together to func, and call it as done below:
void func(A* pA, int af, int bf, void (A::*fc)())
{
...
(pA->*func)();
}
However, if func() has the object on which the member function should be invoked passed in as an argument, it is not clear what is the purpose of af and bf.
Possibly, if your mul() member function does not need to work on any concrete instance of A, and still you want to make it a member of A, you could declare it as static:
class A
{
int a;
int b;
static void mul();
// ^^^^^^
};
This would make your original call to func() compile. However, in this case it is not clear whether this would make sense, since the fact that mul() accept no arguments suggests that it is supposed to work on the a and b members of the object it is invoked on.
Is mul indended to be a non-static member, needing an object of type A to invoke it on? In that case, you'll need to pass a pointer-to-member:
void func(int af, int bf, void (A::*fc)())
{ // ^^^
A a = whatever();
(a.*fc)();
}
If it's intended to be called without an object, then it needs to be static.
What you want to do is to pass a member function pointer (A::mul) along with the instance (a) itself. That's not that easy without any additional code.
Member function pointers aren't tied to an instance (they only represent the function itself, not the instance on which to be called on). Their type is described together with the class name. The type of a member function pointer on A with the signature of your func will look like this:
void (A::*)() // The type only, anonymous
void (A::*fc)() // An actual function pointer with the name `fc`
To call such a function, you have to use the following syntax, in which, as you can see, the instance to call the function on, has to be involved:
(a->*fc)(); // If a is of type A*
(a.*fc)(); // If a is of type A or A&
To put things together, you have basically two options.
Also pass the instance (as a pointer, a reference, either const or modifiable) in addition to a member function pointer. Then, call the member function pointer on that instance.
You're function's signature and implementation will then look like this:
void func(int af, int bf, void (A::*fc)(), A *a) {
// When you want to call the function fc on a:
(a->*fc)();
}
To call func, use a code like this:
func(as.a, as.b, &A::mul, a);
Convert the member function pointer together with the instance to a functor which can be called as is. This can be best done with std::mem_fn together with std::bind1st.
You're function's signature and implementation will then look like this:
void func(int af, int bf, std::function<void()> fc) {
// When you want to call the function fc:
fc();
}
To call func, use a code like this:
func(as.a, as.b, std::bind1st(std::mem_fn(&A::mul), a));
If you really want to treat int (*fc)(int,int) (void return and empty parameter list given in another example) as a regular function pointer (I mean not function pointer to class member function), You can do the following to use the function of A and pass it into func. The point is to declare mul as static member function. I am assuming very simple operation like multiplication.
#include <iostream>
class A
{
public: //necessary otherwise not accessible in main
int a;
int b;
A(int p1, int p2):a(p1),b(p2){} //assume you need to somehow pass a,b into mul
static int mul(int p1, int p2) {return p1*p2;}; //mul should be static
};
void func(int af, int bf, int (*fc)(int,int))
//using void still work but you need to change declaration of mul in A
{
int res = fc(af,bf);
cout << res <<endl;
}
int main()
{
A as(2,4);
func(as.a, as.b, &A::mul); //call A::mul, use as regular function pointer, output 8
func(10,10, &A::mul); //will output 100
return 0;
}
This way you are using the static member function of A that can be pointed to by a regular function pointer. It has nothing to do as a and b inside the class anymore.
As leemes said, you can do this with std::function which is what I would recommend as well, but check out what your compiler has support for, and if so, you can use a single call to std::bind and have it work. Example below that works on Visual Studio 2012 (and should work on newer versions of GCC with the flag for C++11 enabled, and may or may not work on VS 2010)
void func(int af, int bf, std::function<void()> fc)
{
fc();
}
call with:
func(as.a, as.b, std::bind(&as::mul, &A));
The newer versions of std::bind do the std::mem_fcn part for you if it detects a member function. See the doc linked here:
std::function
std::bind

Mixed-Code - Function Member Pointers

I would like to call a FORTRAN function from my C++ code. The FORTRAN function is something like this
extern "C" { void FORTRANFUNC( void FCN(int*,double*), int *N); }
The FCN function reported above is a member function of a C++ class
class CppClass
{
...
void FCN(int* a, double* b);
...
};
I would like to call the code in this way, but it seems that it is not possible:
FORTRANFUNC(myClass.FCN, &n_);
The compiler complains because the FORTRAN functions wants a void FCN function, while I am passing a CppClass::void function.
Can you help me?
Member function pointers are not pointers. They require special handling on the call-site. Since Fortran code is unlikely to know the drill, what you're trying to do is impossible. You must pass a free function instead. Since there is no void* argument for arbitrary user data, the only workaround (and I stress that: this is fugly workaround) you have is to use a global for dispatch (well, or create a thunk dynamically, but that's no so easy):
CppClass *obj;
void dispatch_fcn(int* a, double* b) {
obj->fcn(a, b);
}
// later:
obj = &myClass;
FCN(dispatch_fcn, &n_);

Calling a C++ function pointer on a specific object instance

I have a function pointer defined by:
typedef void (*EventFunction)(int nEvent);
Is there a way to handle that function with a specific instance of a C++ object?
class A
{
private:
EventFunction handler;
public:
void SetEvent(EventFunction func) { handler = func; }
void EventOne() { handler(1); }
};
class B
{
private:
A a;
public:
B() { a.SetEvent(EventFromA); } // What do I do here?
void EventFromA(int nEvent) { // do stuff }
};
Edit: Orion pointed out the options that Boost offers such as:
boost::function<int (int)> f;
X x;
f = std::bind1st(
std::mem_fun(&X::foo), &x);
f(5); // Call x.foo(5)
Unfortunately Boost is not an option for me. Is there some sort of "currying" function that can be written in C++ that will do this kind of wrapping of a pointer to a member function in to a normal function pointer?
You can use function pointers to index into the vtable of a given object instance. This is called a member function pointer. Your syntax would need to change to use the ".*" and the "&::" operators:
class A;
class B;
typedef void (B::*EventFunction)(int nEvent)
and then:
class A
{
private:
EventFunction handler;
public:
void SetEvent(EventFunction func) { handler = func; }
void EventOne(B* delegate) { ((*delegate).*handler)(1); } // note: ".*"
};
class B
{
private:
A a;
public:
B() { a.SetEvent(&B::EventFromA); } // note: "&::"
void EventFromA(int nEvent) { /* do stuff */ }
};
Run away from raw C++ function pointers, and use std::function instead.
You can use boost::function if you are using an old compiler such as visual studio 2008 which has no support for C++11.
boost:function and std::function are the same thing - they pulled quite a bit of boost stuff into the std library for C++11.
Note: you may want to read the boost function documentation instead of the microsoft one as it's easier to understand
I highly recommend Don Clugston's excellent FastDelegate library. It provides all the things you'd expect of a real delegate and compiles down to a few ASM instructions in most cases. The accompanying article is a good read on member function pointers as well.
http://www.codeproject.com/KB/cpp/FastDelegate.aspx
You may find C++ FAQ by Marshall Cline helpful to what you're trying to accomplish.
Read about pointers to members.
To call a method on the derived class, the method has to be declared in the base class as virtual and overriden in the base class and your pointer should point to the base class method. More about pointers to virtual members.
If you're interfacing with a C library, then you can't use a class member function without using something like boost::bind. Most C libraries that take a callback function usually also allow you to pass an extra argument of your choosing (usually of type void*), which you can use to bootstrap your class, as so:
class C
{
public:
int Method1(void) { return 3; }
int Method2(void) { return x; }
int x;
};
// This structure will hold a thunk to
struct CCallback
{
C *obj; // Instance to callback on
int (C::*callback)(void); // Class callback method, taking no arguments and returning int
};
int CBootstrapper(CCallback *pThunk)
{
// Call the thunk
return ((pThunk->obj) ->* (pThunk->callback))( /* args go here */ );
}
void DoIt(C *obj, int (C::*callback)(void))
{
// foobar() is some C library function that takes a function which takes no arguments and returns int, and it also takes a void*, and we can't change it
struct CCallback thunk = {obj, callback};
foobar(&CBootstrapper, &thunk);
}
int main(void)
{
C c;
DoIt(&c, &C::Method1); // Essentially calls foobar() with a callback of C::Method1 on c
DoIt(&c, &C::Method2); // Ditto for C::Method2
}
Unfortunately, the EventFunction type cannot point to a function of B, because it is not the correct type. You could make it the correct type, but that probably isn't really the solution you want:
typedef void (*B::EventFunction)(int nEvent);
... and then everything works once you call the callback with an obhect of B. But you probably want to be able to call functions outside of B, in other classes that do other things. That is sort of the point of a callback. But now this type points to something definitely in B. More attractive solutions are:
Make B a base class, then override a virtual function for each other class that might be called. A then stores a pointer to B instead of a function pointer. Much cleaner.
If you don't want to bind the function to a specific class type, even a base class (and I wouldn't blame you), then I suggest you make the function that gets called a static function: "static void EventFrom A(int nEvent);". Then you can call it directly, without an object of B. But you probably want it to call a specific instance of B (unless B is a singleton).
So if you want to be able to call a specific instance of B, but be able to call non-B's, too, then you need to pass something else to your callback function so that the callback function can call the right object. Make your function a static, as above, and add a void* parameter which you will make a pointer to B.
In practice you see two solutions to this problem: ad hoc systems where you pass a void* and the event, and hierarchies with virtual functions in a base class, like windowing systems
You mention that boost isn't an option for you, but do you have TR1 available to you?
TR1 offers function, bind, and mem_fn objects based on the boost library, and you may already have it bundled with your compiler. It isn't standard yet, but at least two compilers that I've used recently have had it.
http://en.wikipedia.org/wiki/Technical_Report_1
http://msdn.microsoft.com/en-us/library/bb982702.aspx
It's somewhat unclear what you're trying to accomplish here. what is clear is that function pointers is not the way.
maybe what you're looking for is pointer to method.
I have a set of classes for this exact thing that I use in my c++ framework.
http://code.google.com/p/kgui/source/browse/trunk/kgui.h
How I handle it is each class function that can be used as a callback needs a static function that binds the object type to it. I have a set of macros that do it automatically. It makes a static function with the same name except with a "CB_" prefix and an extra first parameter which is the class object pointer.
Checkout the Class types kGUICallBack and various template versions thereof for handling different parameters combinations.
#define CALLBACKGLUE(classname , func) static void CB_ ## func(void *obj) {static_cast< classname *>(obj)->func();}
#define CALLBACKGLUEPTR(classname , func, type) static void CB_ ## func(void *obj,type *name) {static_cast< classname *>(obj)->func(name);}
#define CALLBACKGLUEPTRPTR(classname , func, type,type2) static void CB_ ## func(void *obj,type *name,type2 *name2) {static_cast< classname *>(obj)->func(name,name2);}
#define CALLBACKGLUEPTRPTRPTR(classname , func, type,type2,type3) static void CB_ ## func(void *obj,type *name,type2 *name2,type3 *name3) {static_cast< classname *>(obj)->func(name,name2,name3);}
#define CALLBACKGLUEVAL(classname , func, type) static void CB_ ## func(void *obj,type val) {static_cast< classname *>(obj)->func(val);}