Function pointer is not a function or function pointer

I have the following problem:

void MyClass::LoopFunction(vector<Item>& items,void (MyClass::*funcy)(vector<Item>&,int))
{
    for(SInt32 i = 0; i < 50; i++)
    {
        funcy(items,i);
    }

}

      

It says:

Called object type 'void(MyClass::*)(vector<Item>&,int)' is not a function or function pointer

      

Can anyone help me find a solution for this?

+3


source to share


2 answers


funcy

is a pointer to a member function, so you need to call it on an instance of the class, for example:



(this->*funcy)(items,i);

      

+5


source


The thing with non-static member functions is that they have an invisible first argument that the compiler will turn into this

inside the member function. This means that you cannot call (non-static) member functions without having an instance of an object to call the member function.

In your case, if you want to call it using this

objectinside LoopFunction

, you need to do eg.

(this->*funcy)(items, i);

      



If you want to call it on another instance of an object, you need to pass that object to a function and use it instead.

Or you can use for example. std::function

and std::bind

instead of a member function a pointer. Or use a template parameter similar to how the standard library handles callbacks and which allows any callable object to be passed to a function (still have to be used std::bind

if you want to use a non-stationary member function though).

+2


source







All Articles