c++怎么传递函数

传送门 ==>> AutoSAR实战系列300讲「糖果Autosar」总目录

函数是一组接受输入、执行某些特定计算并产生输出的语句。使用函数的想法是一起执行一些常见或重复完成的任务,以便为不同的输入一次又一次地编写相同的代码。函数的一般形式是:

return_type function_name([ arg1_type arg1_name, ... ]) {

     // 执行操作

}

将函数作为参数传递是C/C++中的一个有用概念。在将自定义比较器函数作为参数传递到std::sort()以根据需要对对象 序列进行排序时,已经使用了这个概念。在本文中,我们将讨论设计接受另一个函数作为参数的函数的不同方法。

1 将指针传递给函数

一个函数也可以通过将其地址传递给另一个函数来传递给另一个函数。下面是C++ 程序:

C++

// C++ program to pass function as a
// pointer to any function
  
#include <iostream>
using namespace std;
  
// Function that add two numbers
int add(int x, int y)
{
    return x + y;
}
  
// Function that multiplies two
// numbers
int multiply(int x, int y)
{
    return x * y;
}
  
// Function that takes a pointer
// to a function
int invoke(int x, int y,
           int (*func)(int, int))
{
    return func(x, y);
}
  
// Driver Code
int main()
{
    // Pass pointers to add & multiply
    // function as required
    cout << "Addition of 20 and 10 is ";
    cout << invoke(20, 10, &add)
         << '\n';
  
    cout << "Multiplication of 20"
         << " and 10 is ";
    cout << invoke(20, 10, &multiply)
         << '\n';
  
    return 0;
}

输出:

Addition of 20 and 10 is 30
Multiplication of 20 and 10 is 200

2 使用 std::function<>

在C++ 11中,有一个 std::function<>模板类允许将函数作为对象传递。std::function<>的对象 可以如下创建。

std::function<return_type(arg1_type, arg2-type…)> obj_name
//  This object can be use to call the function as below
return_type catch_variable = obj_name(arg1, arg2);

下面是说明将函数对象作为参数传递给另一个函数的程序:

C++

// C++ program to illustrate the passing
// of functions as an object parameter
#include <functional>
#include <iostream>
using namespace std;
  
// Define add and multiply to
// return respective values
int add(int x, int y)
{
    return x + y;
}
int multiply(int x, int y)
{
    return x * y;
}
  
// Function that accepts an object of
// type std::function<> as a parameter
// as well
int invoke(int x, int y,
           function<int(int, int)> func)
{
    return func(x, y);
}
  
// Driver code
int main()
{
    // Pass the required function as
    // parameter using its name
    cout << "Addition of 20 and 10 is ";
    cout << invoke(20, 10, &add)
         << '\n';
  
    cout << "Multiplication of 20"
         << " and 10 is ";
    cout << invoke(20, 10, &multiply)
         << '\n';
  
    return 0;
}

输出:

Addition of 20 and 10 is 30
Multiplication of 20 and 10 is 200

3 使用 lambdas

C++ 中的Lambda提供了一种定义内联、一次性、匿名函数对象的方法。这些 lambda 可以在需要将函数作为参数传递的地方定义。下面是说明相同内容的 C++ 程序:

C++

// C++ program to pass the function as
// parameter as a lambda expression
#include <functional>
#include <iostream>
using namespace std;
  
// Function that takes a pointer
// to a function
int invoke(int x, int y,
           function<int(int, int)> func)
{
    return func(x, y);
}
  
// Driver Code
int main()
{
  
    // Define lambdas for addition and
    // multiplication operation where
    // we want to pass another function
    // as a parameter
  
    // Perform Addition
    cout << "Addition of 20 and 10 is ";
    int k = invoke(20, 10,
                   [](int x,
                      int y) -> int {
                       return x + y;
                   });
  
    cout << k << '\n';
  
    // Perform Multiplication
    cout << "Multiplication of 20"
         << " and 10 is ";
    int l = invoke(20, 10,
                   [](int x,
                      int y) -> int {
                       return x * y;
                   });
  
    cout << l << '\n';
  
    return 0;
}

输出:

Addition of 20 and 10 is 30
Multiplication of 20 and 10 is 200
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当我们将函数作为参数传递给另一个函数时,我们可以使用函数指针或函数对象(函数器)来实现。 1. 函数指针: 使用函数指针,我们首先需要定义一个与要传递函数具有相同签名(参数和返回值类型)的函数指针类型。然后,我们可以将函数的名称赋给指针,并将该指针作为参数传递给另一个函数。 例如,假设我们有一个函数 `void foo(int)`,我们希望将其作为参数传递给另一个函数 `void bar(void (*func)(int))`,则可以这样做: ```cpp void foo(int x) { // 函数体 } void bar(void (*func)(int)) { // 调用函数指针所指向的函数 func(42); } int main() { // 将函数指针作为参数传递给另一个函数 bar(foo); return 0; } ``` 2. 函数对象(函数器): C++中的函数对象是可调用对象,可以像函数一样被调用。我们可以定义一个类,并在该类中实现 `operator()` 运算符重载。然后,我们可以创建该类的对象,并将该对象作为参数传递给另一个函数。 例如,假设我们有一个函数对象类 `Foo`,我们希望将其作为参数传递给另一个函数 `void bar(Foo)`,则可以这样做: ```cpp class Foo { public: void operator()(int x) { // 函数体 } }; void bar(Foo func) { // 调用函数对象 func(42); } int main() { // 将函数对象作为参数传递给另一个函数 Foo foo; bar(foo); return 0; } ``` 无论是使用函数指针还是函数对象,我们都可以将函数作为参数传递给其他函数,并在接受参数的函数中调用该函数。这样可以实现更灵活的代码设计和功能扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果Autosar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值