如何定义指向函数的指针,以及如何在C++中使用它

How to define pointer to pointer to function and how to use it in C++?

本文关键字:C++ 定义 何定义 函数 指针      更新时间:2023-10-16

我的问题是如何翻译以下示例?这是一个返回int指针的函数吗?

int* (*function)(int, (int (*k)(int *)));

我可以不写使用它的程序吗?提前感谢!

  • 它是一个函数指针
  • 函数返回一个指向int的指针
  • 函数的第一个参数是int
  • 函数的第二个参数是函数指针k
  • k返回一个int
  • k将指向int的指针作为参数

当然你可以在你的程序中使用它。这并不罕见。我看到了更糟糕的声明。

为了清楚起见,我将您的"函数"重命名为"F"。然后你可以写:

int* (*F)(int, int (*kFunc)(int *) );

备选方案:

typedef int (*kFunc)(int *);
int* (*F)(int, kFunc);

有很多方法可以使用指向函数的指针,可能是像Factory这样的模式可以利用函数指针来创建新对象。(看这里:http://www.codeproject.com/Articles/3734/Different-ways-of-implementing-factories)

也许这段代码可以帮助你,并给出powerfull如何使用函数指针的想法。

#include <stdio.h>
#include <stdlib.h>
#include <map>
// Define the func ptrs
typedef void (*TFunc)(const char *, int);
typedef int (*TFunc2)(int);
int return_value(int i)
{
    return i * 5;
}
void a( const char *name, int i )
{
    printf ("a->%s %dnn", name, i);
}
void b( const char *name, int i)
{
     printf ("b->%s %dnn", name, i);
}
struct test
{
    const char *name;
    int i;
    TFunc func;
};
static test test_array[2] = 
{
    { "a",     0,  a           },
    { "b",     1,  b           },
};
int main(int argc, char **argv, char** envp)
{
    // Check the simple case, pointer to a function
    TFunc fnc = a;
    TFunc2 fnc2 = return_value;
    fnc("blabla", 5);
    fnc = b;
    fnc("hello!", 55);
    printf ("%dnn",fnc2(5));
    //Check arrays of structs when there is a pointer to a fnc
    test_array[0].func(test_array[0].name, test_array[0].i);
    test_array[1].func(test_array[1].name, test_array[1].i);
    //Handle a map of functions( This could be a little implementation of a    factory )
    typedef std::map<int, TFunc > myMap;
    myMap lMap;
    lMap.insert(std::make_pair(5, a));
    lMap.insert(std::make_pair(2, b));
    if( lMap.find( 5 ) != lMap.end() )
    {
        lMap[5]("hello map 5", 1);
    }
    myMap::iterator lItFind = lMap.find(2);
    if( lItFind != lMap.end() )
    {
        lItFind->second("hello map 2", 2);
    }
    return(0);
}

我希望这对你有帮助。

您应该删除多余的括号,这是正确的版本:

    int* (*function)(int, int (*k)(int *));

解释(使用左右规则):

int* (*fn)(int, int (*k)(int *));
fn                               : fn is a
(*fn)                            : pointer
(*fn)(int, int (*k)(int *))      : to a function taking as arguments
                                   - an int and 
                                   - function pointer 
                                     which takes a pointer to int 
                                     and returns int
int* (*fn)(int, int (*k)(int *)) : and returns a pointer to int

下面是一个关于如何使用它的简短例子,你也要求How to define pointer to pointer to function,所以下面也包括了它。

http://coliru.stacked-crooked.com/a/d05200cf5f6397b8

#include <iostream>
int bar(int*) {
    std::cout << "inside barn";
    return 0;
}
int* foo(int, int (*k)(int *)) {
    std::cout << "inside foon";
    k(nullptr);
    return nullptr;
}
int main() {
    int* (*function)(int, int (*k)(int *));
    function = foo;
    function(0, bar);
    // Now, as you asked for, a pointer to pointer to above function
    decltype(function) *pff;
    pff = &function;
    (*pff)(0, bar);
}