boost::function as drop-in replacment for ordinary function pointers?
Hi, is it possible to use boost::function as drop-in replacment for ordinary function pointers - or are there some other solutions? I can't get the code below to work. thx and best regards, Oliver typedef void (*FunctionPointer) ( int); void my_fun( int i) { std::cout << i << std::endl; } class MyFun { public: void operator()( int i) { std::cout << i << std::endl; } }; class Test { public: void f( FunctionPointer fp) { fp( 1); } }; void main() { Test tst; MyFun mf; boost::function< void(int) > f; f = boost::ref( mf); // tst.f( my_fun); // works tst.f( f); // ??? - doesn't compile } -- DSL Komplett von GMX +++ Superg�nstig und stressfrei einsteigen! AKTION "Kein Einrichtungspreis" nutzen: http://www.gmx.net/de/go/dsl
On Feb 4, 2005, at 4:22 AM, Oliver Kowalke wrote:
Hi, is it possible to use boost::function as drop-in replacment for ordinary function pointers - or are there some other solutions? I can't get the code below to work. thx and best regards, Oliver
typedef void (*FunctionPointer) ( int); [snip]
class Test { public: void f( FunctionPointer fp) { fp( 1); } }; [snip] boost::function< void(int) > f; [snip] // tst.f( my_fun); // works tst.f( f); // ??? - doesn't compile }
A boost::function object acts like a function pointer, but it is not a
function pointer. If you change the typedef of FunctionPointer to
typedef boost::function
participants (2)
-
Doug Gregor
-
Oliver Kowalke