如何使用typedef函数指针注册回调函数 [英] How to use a typedef function pointer to register a callback

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

问题描述

我试图用C ++实现一个观察者模式(排序),我想使用函数指针这样做,但是我试图把一个函数指针从类B转换为typedef函数指针时不断收到错误:

  #include< map> 

typedef int(* OutputEvent)(const char *,const char *,int);

class A
{
private:
int nextListenerId;
std :: map< int,OutputEvent> listenerMap;
public:
A(){nextListenerId = 0;}
〜A(){}
inline int RegisterListener(OutputEvent callback)
{
nextListenerId ++ ;
listenerMap [nextListenerId] = callback;
return nextListenerId;
}
};

class B
{
private:
int listenerId;
public:
B(const A& a)
{
OutputEvent e =& B :: CallMeBack;
listenerId = a.RegisterListener(e);
}
〜B(){}

int CallMeBack(const char * x,const char * y,int z)
{
return 0 ;
}
};

我创建了这个例子,我有粘贴到codepad.org ,但是当我无法编译(它不编译在codepad.org或Visual Studio 2010):

 输出:

t.cpp:在构造函数'B :: B(const A&)':
28:错误:无法在初始化时将int(B :: *)(const char *,const char *,int)'转换为'int(*)(const char *,const char *,int)'
编译因-Wfatal-errors而终止。



我不明白为什么它不能转换函数指针。

解决方案

您试图投射到的函数OutputEvent 是一个成员函数。这在错误消息中清楚地表示为:

 'int(B :: *)(const char *,const char *,int)'

这是与

不同的类型

  int(* OutputEvent)(const char *,const char *,int)

因为 B :: 部分(意味着该函数有一个不可见的 this parameter>)。



如果你将成员函数定义为static,那么你可以将它转换为 OutputEvent

  class B 
{
....
static int CallMeBack const char * x,const char * y,int z);
...
};


I'm trying to implement an observer pattern (of sorts) with C++ and I want to use function pointer to do so, but I keep getting an error when trying to cast a function pointer from class B to a typedef function pointer:

#include <map>

typedef int (*OutputEvent)(const char*, const char*, int);

class A
{
private:
    int nextListenerId;
    std::map<int, OutputEvent> listenerMap;
public:
    A(){ nextListenerId = 0;}
    ~A(){}
    inline int RegisterListener(OutputEvent callback)
    {
        nextListenerId++;
        listenerMap[nextListenerId] = callback;
        return nextListenerId;
    }
};

class B
{
private:
    int listenerId;
public:
    B(const A& a)
    {
        OutputEvent e = &B::CallMeBack;
        listenerId = a.RegisterListener(e);
    }
    ~B(){}

    int CallMeBack(const char* x, const char* y, int z)
    {
        return 0;
    }
};

I created this example and I've pasted it into codepad.org, but when I it fails to compile (it doesn't compile in codepad.org nor in Visual Studio 2010):

Output:

t.cpp: In constructor 'B::B(const A&)':
Line 28: error: cannot convert 'int (B::*)(const char*, const char*, int)' to 'int (*)(const char*, const char*, int)' in initialization
compilation terminated due to -Wfatal-errors.

I don't understand why it can't convert the function pointers. Could anybody help me please?

解决方案

The function you are trying to cast to OutputEvent is a member function. This is represented clearly in the error message by this:

'int (B::*)(const char*, const char*, int)'

which is a different type than

 int (*OutputEvent)(const char*, const char*, int)

because of the B:: part (that means that the function has an invisible this parameter).

If you define your member function as static, then you will be able to cast it to OutputEvent:

class B
{
   ....
   static int CallMeBack(const char* x, const char* y, int z);
   ...
 };

这篇关于如何使用typedef函数指针注册回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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