C++ 中的代表是什么意思?

发布于 2024-08-16 04:00:07 字数 60 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秋叶绚丽 2024-08-23 04:00:07

“委托”实际上并不是 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<> with boost::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.

·深蓝 2024-08-23 04:00:07

C++ 中的回调函数可以(宽松地)称为委托的一种形式(尽管此处不使用委托术语)。回调函数使用函数指针将它们作为参数传递给其他函数。
但与 C++ 中的回调函数相比,C# 中的委托更为高级。

Callback functions in C++ can be (loosely) referred as a form of delegates ( though delegate 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++.

香草可樂 2024-08-23 04:00:07

委派工作意味着与他人分担工作量。在现实生活中,如果您要委派您的任务,即如果您是一名经理,您将分享您的工作,期望其他人完成任务,而您不必知道如何完成。

这个概念在 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.

优雅的叶子 2024-08-23 04:00:07

委托主要用作将函数传递给类中嵌入的功能的一种方式(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.

和我恋爱吧 2024-08-23 04:00:07

委托:一个对象,其作用类似于具有订阅系统的多功能指针。它确实简化了用于回调通知和事件处理的静态或“对象”成员函数指针的使用。

链接以清晰的方式解释代表或您还可以参考 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文