https://stackoverflow.com/questions/9568150/what-is-a-c-delegateexpress
You have an incredible number of choices to achieve delegates in C++. Here are the ones that came to my mind.flex
Option 1 : functors:this
A function object may be created by implementing operator()
code
struct Functor { // Normal class/struct members int operator()(double d) // Arbitrary return types and parameter list { return (int) d + 1; } }; // Use: Functor f; int i = f(3.14);
Option 2: lambda expressions (C++11 only)orm
// Syntax is roughly: [capture](parameter list) -> return type {block} // Some shortcuts exist auto func = [](int i) -> double { return 2*i/1.15; }; double d = func(1);
Option 3: function pointersip
int f(double d) { ... } typedef int (*MyFuncT) (double d); MyFuncT fp = &f; int a = fp(3.14);
Option 4: pointer to member functions (fastest solution)get
See Fast C++ Delegate (on The Code Project).it
struct DelegateList { int f1(double d) { } int f2(double d) { } }; typedef int (DelegateList::* DelegateType)(double d); DelegateType d = &DelegateList::f1; DelegateList list; int a = (list.*d)(3.14);
Option 5: std::functionio
(or boost::function
if your standard library doesn't support it). It is slower, but it is the most flexible.ast
#include <functional> std::function<int(double)> f = [can be set to about anything in this answer] // Usually more useful as a parameter to another functions
Option 6: binding (using std::bind)
Allows setting some parameters in advance, convenient to call a member function for instance.
struct MyClass { int DoStuff(double d); // actually a DoStuff(MyClass* this, double d) }; std::function<int(double d)> f = std::bind(&MyClass::DoStuff, this, std::placeholders::_1); // auto f = std::bind(...); in C++11
Option 7: templates
Accept anything as long as it matches the argument list.
template <class FunctionT> int DoSomething(FunctionT func) { return func(3.14); }