Home  >  Article  >  Backend Development  >  How to pass and call functions using C++ function pointers?

How to pass and call functions using C++ function pointers?

王林
王林Original
2024-04-17 16:36:01931browse

Function pointers allow functions to be passed as parameters, making function calls more flexible. You declare a function pointer, pass parameters, and call the pointed function through pointer operators. Advanced functions such as dynamic scheduling and sorting algorithm selection can be implemented through function pointers.

如何使用 C++ 函数指针传递和调用函数?

How to use C function pointers to pass and call functions

A function pointer is a special type of pointer that points to a function . Function pointers allow us to pass functions as parameters to other functions, enabling higher-level function calls.

Function pointer declaration and initialization

To declare a function pointer, we need to specify the return type and parameter type of the function:

int (*func_ptr)(int, int);

The above code declares A function pointer func_ptr that points to a function that receives two integers and returns an integer.

To initialize a function pointer, we can use the function name or the function pointer:

func_ptr = my_function;  // 使用函数名
func_ptr = &my_function; // 使用函数指针

where my_function is the function with the corresponding signature.

Passing function pointers

You can pass function pointers as parameters to other functions:

void call_function(int (*func)(int, int)) {
    int result = func(1, 2);
    // ...
}

The above function call_function receives a Function pointer func, which points to a function that receives two integers and returns an integer.

Calling a function pointer

To call a function pointed to by a function pointer, we need to use the pointer operator (*):

int result = (*func_ptr)(1, 2);  // 调用通过 func_ptr 指向的函数

Practical Case

Consider the following example of sorting an array using function pointers:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int compare(int a, int b) {
    return a > b;
}

bool greater_than(int a, int b) {
    return a > b;
}

int main() {
    vector<int> arr = {1, 4, 2, 8, 5};

    // 使用函数指针 compare 降序排序
    sort(arr.begin(), arr.end(), compare);

    // 使用 lambda 表达式升序排序
    sort(arr.begin(), arr.end(), [](int a, int b) {
        return a < b;
    });

    // 使用 std::greater 升序排序
    sort(arr.begin(), arr.end(), greater<int>());

    for (int i = 0; i < arr.size(); i++) {
        cout << arr[i] << " ";
    }

    return 0;
}

In the above example, we defined three comparison functions:

  • compare: Descending comparison function
  • greater_than: Ascending comparison function (using function pointer)
  • [ ](int a, int b) { return a < b; }:Ascending comparison function (using lambda expression)

Functionsort uses a function pointer as Argument to sort the array according to the specified comparison function. This example demonstrates how to use function pointers to easily switch between different sorting algorithms.

The above is the detailed content of How to pass and call functions using C++ function pointers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn