转自:http://stackoverflow.com/questions/9568150/what-is-a-c-delegate
Option 1 : functors:
A function object may be created by implementing operator()
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)
// 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 pointers
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)
See Fast C++ Delegate (on The Code Project).
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::function
(or boost::function
if your standard library doesn't support it). It is slower, but it is the most flexible
#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
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); }
相关推荐
网上有很多关于C++ delegate机制的文章,但都讲的是函数指针的内容,上周就C++中实现C#的delegate机制的问题研究了好几天,查看了很多相关资料,才解决了问题,现将我写的C++ delegate测试程序贴出来,希望能帮到有...
用C++做项目的时候,尤其是写客户端的时候经常会有事件回调的设计,一般的方式是使用虚函数表,用一个虚基类包含...但这种方式和C++11的lamda不兼容,为了更方便的实现事件回调机制,使用delegate是很不错的一种方式。
C++下实现委托的代码,利用了些编程技巧和大家分享~~
NULL 博文链接:https://cooker.iteye.com/blog/1176141
delegate_execute.exe
C# Delegate讲解C# Delegate讲解C# Delegate讲解C# Delegate讲解
对iOS的delegate代理方式不是很清楚的同学可以来看看,很简单的代码,看过之后应该给你有帮助
本例子演示了iPhone开发中最常用到的两个类之间互相通信的方法之一:delegate
ios设计模式delegate 示例 ,导航功能的两个视图控制器(用push方式从一个窗口推到另一个窗口),第二个窗口创建两个按钮,标题分别为红色和绿色,点相应的按钮弹回前一个窗口,同时前一个窗口的背影色变成按钮标题...
delegate、五步曲、调用照相机、切换键盘
delegate_exe
C++下多模匹配算法:Wu Manber的实现 ... It all needs to be redone a bit in order to accept a 'functor' or a 'delegate' so that when a match is found, an appropriate routine can be invoked.
Allows a low-privileged COM client to delegate calls to a COM server that is running under a higher-priveleged NT user account.(41KB)
CSharp_Delegate C#委托 本人博客中的示例代码
ios applicationDelegate使用;ios applicationDelegate使用
网上也有很多Delegate的Demo,比较乱,感觉不容易看懂。本来一个很简单的问题,大家搞得这么复杂。这个Demo就通俗易懂、简单、实用
oc中以delegate实现页面间的传值过程
delegate进行GameObject之间的通信.pdf
适合初学者了解C#的delegate,是一个非常简单的例子。
C# delegate thread范例 若不懂在代码中,线程如何调用的可以看看