C ++调用静态函数指针 [英] C++ calling static function pointer

查看:339
本文介绍了C ++调用静态函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将回调转发给函数指针.所以我声明了一个静态(int *)m_pointer1,以及一个方法void RegisterCallback1((int *)fct)

I would like to forward a callback to a function pointer. So I declare a static (int*) m_pointer1, as well as a method void RegisterCallback1( (int*)fct)

在class1.h中:

in class1.h:

public:
   int RegisterCallback1( int (*fct) );
private:
   static int (*m_Callback1);

在class1.cpp中:

in class1.cpp:

int class1::RegisterCallback1( int (*fct) )
{
    m_Callback1= fct;
}

然后,我想将回调转发到函数指针:

then, I want to forward the callback to the function pointer:

void class1::Callback1()
{
   (*m_Callback1)();
}

但是我收到一个编译器错误表达式必须具有(指针指向)-函数类型 我已经按照教程学习了有关函数指针的内容,他们似乎以这种方式完成了操作,而没有任何问题.有什么想法吗?

But I get a compiler error "Expression must have (pointer-to)- function type I have followed tutorial and read about function pointers and they seem to do it this way without any problems. Any ideas why?

因此,我声明(int *)(void)m_Callback1 -Visual Studio在此处需要一个空值...- 那我怎么用参数调用registerCallback函数呢?

So, I declare (int*)(void)m_Callback1 -Visual Studio requires a void there...- Then how do I call the registerCallback function with the argument?

class1.RegisterCallBack1(  ??? - class2::callback -??? );

推荐答案

您尚未声明函数指针,而已声明普通数据指针.您在声明的末尾缺少().

You haven't declared a function pointer, you've declared a normal data pointer. You are missing () at the end of the declaration.

这篇关于C ++调用静态函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆