C++ 如何在c+;中创建和使用带有alternativ parameterlist的functionpointer+;班

C++ 如何在c+;中创建和使用带有alternativ parameterlist的functionpointer+;班,c++,function-pointers,C++,Function Pointers,返回另一个函数指针问题;) 我想在我的类中创建一个函数指针,它可以指向具有不同参数列表的不同函数。也就是说,我想要一个functionpointer,它可以指向并调用以下所有函数(及其resp参数) funct1接受一个函数指针并 使用指向func3的指针调用func2 func2调用func3 在C代码中,它们声明为: typedef int (*FUNCTION)(); int func1(long sec, FUNCTION func, int argc, long *

返回另一个函数指针问题;)

我想在我的类中创建一个函数指针,它可以指向具有不同参数列表的不同函数。也就是说,我想要一个functionpointer,它可以指向并调用以下所有函数(及其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); }
typedef int(cDP5::*FUNCTION)(int *fc, char *buf, int sz);
现在我创建了一个类cDP5来保存这些方法和函数指针 我是这样宣布的

func1(b->argv[0], func2, 3, arglist){ func(func3); }
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)}’
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)}’
但是我得到了这个错误:

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)}’
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++类中创建和使用所需的函数指针?
或者我需要为每个参数列表创建一个吗?

好的,根据Schiff提到的内容和我自己的研究,我发现创建一个包含多个参数列表的函数指针是不可能的。 我必须为每个参数列表创建functionpointer


虽然我在中找到了一个使用模板参数的选项,但我认为这会在某种程度上增加参数类型;)

你在C中的第一次呼叫看起来有点错误。顺便说一句,
func1
的第二个参数取一个函数指针,其中
func3
的签名匹配,而
func2
的签名不匹配。如果您使用
func2
作为第二个参数调用
func1
。那么这将导致一个类型不匹配错误。嗨,谢夫,我完全同意你的看法,但这是可行的。!!??我不知道如何,因为代码是从80-90:第二,这可能是原因。对于错配错误,它是真实的C++代码,因为它是新的。我是否要接受你的评论,因为答案是,我必须针对每种情况(不同的参数列表)创建一个指针?关于C:C编译器在类型检查方面要宽松得多。K&R C几乎没有进行任何打字检查。(Protos甚至并没有发明。)这一点随着不同的标准一步步改变。关于C++:存储函数指针还是方法指针是一个真正的区别。至多,函数指针可以处理
静态
类方法。大多数API(例如,
std::function
,涵盖两者)都是以性能为代价来实现这一点的。(当我在做一个简单的信号/插槽实现时,我曾经深入探讨过这个主题。)聪明的替代方案通常依赖于平台。不幸的是,我手头没有有用的链接,也无法通过谷歌快速找到它们。。。