gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:09:00 27 4
gpt4 key购买 nike

定义(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*, char*, key*, key*)' does not match 'int (*)(int*, char*, key*, key*)'

指针使用:

(*foolink)(NULL, 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++ 常见问题解答:

Pointers to Member Functions

关于c++ - 类初始化时如何用非静态类函数初始化静态函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5853052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com