C++ How to create a function which takes as argument a reference to a function pointer?

与世无争的帅哥 提交于 2019-12-13 18:09:10

问题


i'm using function pointers typedef'd:

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 function);

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

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.

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

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 ?

Thanks in advance! :)

Edit:

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; } }

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...


回答1:


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);

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




回答2:


ptr2Func*& func

looks horrible, reference to function pointer to function pointer?

Working code:

#include <iostream>
#include <vector>
using namespace std;

typedef int(*ptr2Func)();

int zero() { return 10; }
int one() { return 11; }
int two() { return 12; }
int three() { return 13; }

const ptr2Func functions[] = { zero, one, two, three };

//possible other logic
ptr2Func getPtr2Func(int number) { return functions[number]; }

//possible other logic (std::map, whatever)
int getPtr2FuncNum(ptr2Func func)
{
    for (size_t i = 0; i < sizeof(functions) / sizeof(*functions); ++i)
        if (functions[i] == func)
            return i;
    return -1;
}

int main()
{
    ptr2Func p = getPtr2Func(2);
    cout << p() << '\n'; //12
    cout << getPtr2FuncNum(p) << '\n'; //2

    return 0;
}

Output:

12
2



回答3:


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; } }

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...



来源:https://stackoverflow.com/questions/4684576/c-how-to-create-a-function-which-takes-as-argument-a-reference-to-a-function-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!