返回另一个函数指针问题;)
我想在我的类中创建一个函数指针,它可以指向具有不同参数列表的不同函数。也就是说,我想要一个函数指针,它可以指向并调用下面所有的函数(及其resp参数)。
funct1接受函数指针和
使用指向func3的指针调用func2,
func2调用func3
在C代码中,它们被声明为:
typedef int (*FUNCTION)();
int func1(long sec, FUNCTION func, int argc, long *argv);
int func2(FUNCTION func);
int func3();并像这样调用
func1(b->argv[0], func2, 3, arglist){ func(func3); }现在,我已经创建了一个类cDP5来保存这些方法和我声明的函数指针,如下所示
typedef int(cDP5::*FUNCTION)(int *fc, char *buf, int sz);但在此调用中获取编译错误
func1(b->argv[0], func2, 3, arglist);收到此错误:
cDP5.cpp:725:48: error: no matching function for call to ‘cDP5::func1(long&, <unresolved overloaded function type>, int, long int [3])’
cDP5.cpp:725:48: note: candidate is:
In file included from cDP5.cpp:11:0:
cDP5.h:96:9: note: int cDP5::func1(long int, cDP5::FUNCTION, int, long int*)
cDP5.h:96:9: note: no known conversion for argument 2 from ‘<unresolved overloaded function type>’ to ‘cDP5::FUNCTION {aka int (cDP5::*)(int*, char*, int)}’好的,我想如果我尝试这样做一个函数类型转换,那么
func1(b->argv[0], (cDP5::FUNCTION)func2, 3, arglist);但是我得到了这个错误:
cDP5.cpp:725:45: error: cannot convert ‘cDP5::func2’ from type ‘int (cDP5::)(int, long int*)’ to type ‘cDP5::FUNCTION {aka int (cDP5::*)(int*, char*, int)}’你知道如何在c++类中创建和使用想要的函数指针吗?或者我需要为每个参数列表创建一个?
发布于 2017-03-18 16:27:14
好的,根据Schiff提到的和我自己的研究,我发现创建一个接受多个参数列表的函数指针是不可能的。我必须为每个参数列表创建函数指针。
虽然有一个选项可以使用我在template function pointer parameter in a class method中找到的模板参数,但我认为它可以在一定程度上简化参数类型;)
https://stackoverflow.com/questions/42785072
复制相似问题