类初始化时如何用非静态类函数初始化静态函数指针?

问题描述

| 定义(Core.h):
static int (*foolink)(int*,char*,key*,key*);
还可以在Core.cpp中重新定义。此代码导致错误:
foolink = this->step;
错误:
Engine/Core.cpp:31: error: argument of type \'int (Core::)(int*,key*)\' does not match \'int (*)(int*,key*)\'
指针使用:
(*foolink)(NULL,NULL,NULL);
怎么了?请帮我!     

解决方法

在C ++程序中,大多数函数是成员函数。也就是说,它们是课程的一部分。不允许使用普通的函数指针指向成员函数。相反,您必须使用成员函数指针。 就您而言,您可以将其定义为
     v you have to name the class here
int (YourClass::*foolink)(int*,char*,key*,key*);
foolink = &YourClass::step;

// This is how you can call the function via member function pointer
YourClass object,*pObject = &object;
// One way is to envoke the function from object
(object.*foolink)(...);
// The other way is from pointer to object
(pObject->*foolink)(...);
C ++常见问题解答: 指向成员函数的指针     ,
this->step
的类型必须是返回整数并以int *,char *,key *和key *作为参数的函数。显然不是。记住,将类方法分配给普通函数是行不通的。它们都必须是方法,或者都必须是正常函数,但不是必须混合使用,这是我怀疑您要尝试做的事情。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...