By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

drop-in replacment for function pointers

P: n/a
Hello,
I've to use a function from c-library (third party) which expects a
function pointer (void(*FunctionPointer)(int)). Instead of using a
global function (with the signature of the function pointer) I'de like
to use a functor (in order to provide a statefull function). Is this
possible? If yes how?
thx,
Oliver
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Oliver Kowalke" <ol************@gmx.de> wrote...
I've to use a function from c-library (third party) which expects a
function pointer (void(*FunctionPointer)(int)). Instead of using a
global function (with the signature of the function pointer) I'de like
to use a functor (in order to provide a statefull function). Is this
possible? If yes how?


It's possible if you define a class with static data members (your
state is kept there) and a static function member, a pointer to which
you're going to give to the c-library. It's not the same as functor,
of course. Essentially it is very much like keeping your state in
a set of global variables. It's a bit better, still.

The biggest difference between functors who keep their state in their
non-static data members and a class with static data is that you can
have "unlimited" number of functors running around and only one class
"instance". You can overcome this by having multiple instances of the
class (using templates), but it's a bit trickier than functors which
you can instantiate as needed.

V
Jul 23 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:k4********************@comcast.com...
"Oliver Kowalke" <ol************@gmx.de> wrote...
I've to use a function from c-library (third party) which expects a
function pointer (void(*FunctionPointer)(int)). Instead of using a
global function (with the signature of the function pointer) I'de like
to use a functor (in order to provide a statefull function). Is this
possible? If yes how?


It's possible if you define a class with static data members (your
state is kept there) and a static function member, a pointer to which
you're going to give to the c-library. It's not the same as functor,
of course. Essentially it is very much like keeping your state in
a set of global variables. It's a bit better, still.

The biggest difference between functors who keep their state in their
non-static data members and a class with static data is that you can
have "unlimited" number of functors running around and only one class
"instance". You can overcome this by having multiple instances of the
class (using templates), but it's a bit trickier than functors which
you can instantiate as needed.

V


Should it not be possible to use boost::bind to create a function with a
void (*func)(int) signature..?
Jul 23 '05 #3

P: n/a
"Jesper Madsen" <ba***@mail.stofanet.dk> wrote...
[..]
Should it not be possible to use boost::bind to create a function with a
void (*func)(int) signature..?


Could as well be. I don't know Boost. It's not part of Standard C++.
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.