Delegates in C++
Those who experimented with passing functions as arguments in C++ know that there are 2 solutions: function pointers and function objects. Most of us think function pointers have an ugly syntax and you need to know the class instance if you are pointing to a non-static member function... Function objects on the other site are not flexible enough, they require much more typing effort then regular functions. The solution that I thought of was using templates to create a small wrapper around function pointers.
class Test { public: void some_useless_crap(int a, int b) { std::cerr << (a + b) << std::endl; } }; int main(int argc, char** argv) { Test* t = new Test; Km::Functor function(t, &test::some_useless_crap); function(2, 3); }
One drawback of the current version of the wrapper: you should copy/paste it for function with more or less arguments... I'm working on a solution for this, either with macro's or a code generating program. Download
This problem is solved in my delegate implementation which is available now. Download
- Personal tools - Log in -
- Special - Special pages -