C ++如何创建一个以对函数指针的引用作为参数的函数? [英] C++ How to create a function which takes as argument a reference to a function pointer?

查看:106
本文介绍了C ++如何创建一个以对函数指针的引用作为参数的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用typedef'd的函数指针:

i'm using function pointers typedef'd:

typedef int(* ptr2Func)();

typedef int(*ptr2Func)();

还有一个函数,当给定一个数字时,它将为已经声明的函数选择一个函数指针.

And a function which when given a number will select a function pointer for a function that is already declared.

ptr2Func getPtr2Func(int函数);

ptr2Func getPtr2Func(int function);

因此,如果您使用getPtr2Func(1);您会获得指向第一个函数的函数指针.

So if u use getPtr2Func(1); u get a function pointer to the first function.

但是,我现在想做完全相反的事情:将一个函数指针引用传递给一个函数,该函数将依次返回该函数的编号. 即:我有4个测试功能,除了显示一条消息外什么也不做. 如果getPtr2Func(1);调用,将获得指向function1的函数指针.

However i want now to do the exact reverse thing: Pass a function pointer reference into a function which will in turn return the number of the function. i.e: i got 4 test functions which do nothing other than display a message. if getPtr2Func(1); is called, a function pointer to function1 is obtained.

我可以这样做吗? int getFuncNum(ptr2Func *& func);

Can i do this: int getFuncNum(ptr2Func*& func);

从而获得该函数指针指向的函数的编号?假设我知道所有映射到getPtr2Func中给定数字的函数,我可以使用切换用例或多个if-else来查找那些函数的哪个地址与作为参数传递的函数指针匹配,否?

And thus get the number of the function that this function pointer points to? Assuming i know all the functions which are mapped to the number given into getPtr2Func, I could use a switch case or multiple if-else to find which address of those functions matches the function pointer passed as argument, no ?

这样做的正确语法是什么?

Also what would the correct syntax for doing so be ?

提前谢谢! :)

没关系,我找到了答案...

Nevermind i found the answer...

只需按原样传递并比较地址...

Just passing it as it is and comparing addresses...

typedef int(*ptr2Func)();

int findFunc(ptr2Func func){ if (func == &test1){ return 1; } }

我做过一些次要测试...(test1()是一个函数...)尽管不知道有没有更有效或更安全的方法来实现此目的...

Works with some minor testing i did... (test1() is a function...) Don't know if there is a more efficient or safer way to do this though...

推荐答案

要了解这种东西是如何工作的,最好看看回调管理器是如何构建的.通常,当您编写这样的回调函数时,您将传递实例和方法的静态地址.您将要创建一个类似于以下内容的回调管理器:

To understand how this kind of stuff works, it's best to look at how a callback manager is built. Usually when you write a callback function like this you pass the instance and the static address of the method. You'll want to create a callback manager that looks something like this:

...

class MyCallbackClass
{

    public:
       typedef void (MyClass::*CallbackFunction)(int arg);
       MyCallbackClass(MyClass * callbackObj, void(CallbackFunction callbackFunction) 
       {
           mCallbackObj = callbackObj;
           mCallbackFunction = callbackFunction;
       };
    private:
       MyClass * mCallbackObj;
       CallbackFunction mCallbackFunction;

};

...然后在主类中添加一个回调函数...

...then add a callback function into your main class...

// The callback -- outputs "3" (see below)
void MyClass::myFunc(int arg)
{
    cout << arg << "\n";
}

...然后创建回调管理器的实例

...then create an instance of the callback manager

// Implementation
MyCallbackClass myCallback = new MyCallbackClass(this, &MyClass::myFunc);

...您的回调管理器现在可以在您传入的实例/方法上调用函数...

...your callback manager can now call a function on the instance/method you passed in...

// (call this from the instance of MyCallbackClass)
(mCallbackObj->*mCallbackFunction)(3);

此代码都是在stackoverflow上手写的,因此不要指望它可用于复制/粘贴.

This code is all hand written right here at stackoverflow so don't expect it to work with a copy/paste.

这篇关于C ++如何创建一个以对函数指针的引用作为参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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