如何定义指向函数的指针,以及如何在C++中使用它
How to define pointer to pointer to function and how to use it in C++?
我的问题是如何翻译以下示例?这是一个返回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);
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何定义一个纯抽象基类