C++ 中的代表是什么意思?
C++ 中的委托是什么意思,C/C++ 中以比较函数/函子作为最后一个参数的排序函数是否是委托的一种形式?
What is mean by delegates in c++, does sort function in c/c++ which takes a compare function/functor as last parameter is a form of delegate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“委托”实际上并不是 C++ 术语的一部分。在 C# 中,它类似于一个美化的函数指针,它可以存储对象的地址并调用成员函数。您当然可以在 C++ 中编写类似的内容作为一个小型库功能。或者更通用:将
boost::bind<>
与boost::function<>
结合起来。在 C++ 中,我们使用术语“函数对象”。函数对象是可以通过函数调用operator()“调用”的任何东西(包括函数指针)。
std::sort 采用“谓词”,它是一个特殊的函数对象,不会修改其参数并返回布尔值。
"delegate" is not really a part of the C++ terminology. In C# it's something like a glorified function pointer which can store the address of an object as well to invoke member functions. You can certainly write something like this in C++ as a small library feature. Or even more generic: Combine
boost::bind<>
withboost::function<>
.In C++ we use the term "function object". A function object is anything (including function pointers) that is "callable" via the function call operator().
std::sort takes a "predicate" which is a special function object that doesn't modify its arguments and returns a boolean value.
C++ 中的回调函数可以(宽松地)称为委托的一种形式(尽管此处不使用委托术语)。回调函数使用函数指针将它们作为参数传递给其他函数。
但与 C++ 中的回调函数相比,C# 中的委托更为高级。
Callback functions in C++ can be (loosely) referred as a form of
delegates
( thoughdelegate
term is not used for this). The callback functions use Pointers to Functions to pass them as parameters to other functions.But delegates in C# is more advanced compared to callback functions in C++.
委派工作意味着与他人分担工作量。在现实生活中,如果您要委派您的任务,即如果您是一名经理,您将分享您的工作,期望其他人完成任务,而您不必知道如何完成。
这个概念在 C++ 和任何其他具有委托功能的语言中是相同的。在 C 中,您可以将其视为委托:
intcalculate(int (*func)(int c), int a, int b)
因为您需要发送一个指针到另一个函数,该函数将为你计算一些工作。我最近写了一个 关于 Python 和 C 中函数指针的博客文章,请查看,您可能会发现它很有帮助。这可能不是 C 或 C++ 中委派工作的“传统”方式,但话又说回来,术语说我是对的。
To delegate work means to share the work load with others. In real life, if you were to delegate your task, ie if you are a manager, you would be sharing your work expecting others to complete a task without you having to know how.
The concept is the same in C++ and any other languages having the capability of delegates. In C you could see this as a delegate:
int calculate(int (*func)(int c), int a, int b)
Because you are expected to send a pointer, to another function which will compute some work for you. I recently wrote a blog post on function pointers in Python and C, check it out, you might find it helpfull. This might not be the "traditional" way to delegate work in C or C++, but then again, the termonoligy says i am a bit right.
委托主要用作将函数传递给类中嵌入的功能的一种方式(pimpl、聚合、私有继承)。它们主要是一行(内联)函数,调用成员类的函数。据我所知,它与C#的委托无关。
从这个意义上说,qsort 中使用的函数指针不是委托,而是回调,其中框架模块可以由用户软件扩展,如 好莱坞原则。
Delegation is mostly used as a way to pass functions to functionality embedded in a class (pimpl, aggregation, private inheritance). They are mainly (inlined) functions of one line, calling functions of member-classes. As far as I know, it has nothing to do with C#'s delegates.
In this sense, a function-pointer as used in qsort is not a delegate, but a callback in which framework modules can be extended by user-software as in the Hollywood principle.
委托:一个对象,其作用类似于具有订阅系统的多功能指针。它确实简化了用于回调通知和事件处理的静态或“对象”成员函数指针的使用。
此链接以清晰的方式解释代表或您还可以参考 MSDN 链接。
Delegate: An object that acts like a multi-function pointer with a subscription system. It really simplifies the use of static or 'object' member function pointers for callback notifications and event handling.
This link explains Delegates in a lucid manner or you may also refer to the MSDN link.