将函数名称作为参数传递给函数

发布于 2024-11-15 15:53:18 字数 132 浏览 4 评论 0原文

是否可以将一个函数的名称(例如A)作为参数传递给另一个函数(例如B),然后从函数B调用函数A。也就是说,函数名称将存储在B中的变量中,并使用它调用名称在变量中的函数。例如,在 C++ 排序函数中,第一个和第二个参数是迭代器,但第三个参数是函数的名称。

Is it possible to pass the name of a function(say A) as an argument to another function (say B), and then call function A from function B. That is the function name will be stored in a variable in B, and using it call the function whose name is in the variable. For Example in C++ sort function the first and second arguments are iterators, but the third argument is the name of a function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

晨敛清荷 2024-11-22 15:53:18

您可以为“任何可以用两个参数调用的东西”引入一个模板参数(这里是 Pred):

template <typename Iter, typename Pred>
void mysort(Iter begin, Iter end, Pred predicate)
{
    --end;
    // ...
        if (predicate(*begin, *end))
        {
            // ...
        }
    // ...
}

然后您可以传递好的旧 C 函数指针或 C++ 函数对象:

bool function(int x, int y)
{
    return x < y;
}

struct function_object
{
    bool operator()(int x, int y)
    {
        return x < y;
    }
};

int main()
{
    int numbers[] = {543, 6542654, 432514, 54, 45, 243};
    mysort(numbers + 0, numbers + 6, &function);
    mysort(numbers + 0, numbers + 6, function_object());
}

如您所见,一个函数object 是适当重载operator() 的类的对象。

You can introduce a template parameter (here Pred) for "anything that is callable with two parameters":

template <typename Iter, typename Pred>
void mysort(Iter begin, Iter end, Pred predicate)
{
    --end;
    // ...
        if (predicate(*begin, *end))
        {
            // ...
        }
    // ...
}

Then you can pass either good old C function pointers or C++ function objects:

bool function(int x, int y)
{
    return x < y;
}

struct function_object
{
    bool operator()(int x, int y)
    {
        return x < y;
    }
};

int main()
{
    int numbers[] = {543, 6542654, 432514, 54, 45, 243};
    mysort(numbers + 0, numbers + 6, &function);
    mysort(numbers + 0, numbers + 6, function_object());
}

As you can see, a function object is an object of a class that overloads operator() appropriately.

末蓝 2024-11-22 15:53:18

您应该阅读函数指针

You should read up on Function Pointers.

骑趴 2024-11-22 15:53:18

函数指针的简单示例:

#include <iostream>

int apply(int (*fun)(int,int), int a, int b) {
    return (*fun)(a,b);
}

int add(int a, int b) {return a + b;}
int multiply(int a, int b) {return a * b;}

int main(int argc, const char* argv[]) {
    int added = apply(add, 2, 4);
    int multiplied = apply(multiply, 2, 4);

    std::cout << "added result: " << added << std::endl;
    std::cout << "multiplied result: " << multiplied << std::endl;
}

输出:

added result: 6
multiplied result: 8

simple example with Function Pointers:

#include <iostream>

int apply(int (*fun)(int,int), int a, int b) {
    return (*fun)(a,b);
}

int add(int a, int b) {return a + b;}
int multiply(int a, int b) {return a * b;}

int main(int argc, const char* argv[]) {
    int added = apply(add, 2, 4);
    int multiplied = apply(multiply, 2, 4);

    std::cout << "added result: " << added << std::endl;
    std::cout << "multiplied result: " << multiplied << std::endl;
}

output:

added result: 6
multiplied result: 8
独留℉清风醉 2024-11-22 15:53:18

是的,不仅可以,而且用途也很大。

从技术上讲,这些被称为函数指针

从概念上讲,它们需要具有回调机制。 事件驱动程序可能需要回调。例如,您的应用程序可能有兴趣知道何时单击鼠标按钮。在这种情况下,您将向底层平台注册您对鼠标单击事件的兴趣,并告诉它应该在您的程序中调用什么方法/函数,以便您可以相应地执行代码。

另一种情况是程序具有异步执行模式。例如,如果要将文件写入硬盘,与进行算术计算相比,这是一个耗时的过程。因此,我们可能不想在将结果写入文件时等待执行计算。程序可能选择只在文件上调用 write 函数并返回并开始计算。同时,调用此函数时,调用者还可以指定一个回调函数。因此,当文件写入成功或失败时,回调函数将被调用,并相应地通知被调用者。

Yes, not only is it possible, but has great usage as well.

Technically, these are known as function pointers.

Conceptually, they are required to have callback mechanisms. Callbacks might be required for event-driven programs. E.g., your application might be interested to know when a mouse-button is clicked. In this case you would register your interest to the underlying platform for a mouse click event, and tell it what method/function should be called in your program, so that you can execute code accordingly.

Another such case is where programs have an asynchronous mode of execution. E.g., in case a file is to be written to hard disk, which is a time consuming process as compared to doing arithmetical calculations. So, it might be a case where we do not want to wait performing calculations while we are writing results to a file. A program might choose to just call a write function on a file and return and start calculations. While, calling this function the caller might also specify a callback function. So, when the file is written successfully or fails, the callback function is called and the callee informed accordingly.

不乱于心 2024-11-22 15:53:18

是的,这是可能的。从技术上讲,它并不完全是传递的“名称”,而是实际上是指向所传递函数的指针。

Yes, it's possible. Technically, it's not quite the 'name' that's passed, but rather, actually a pointer to the function being passed.

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