Search code examples
c++function-pointers

Non-static class function pointer


I am trying to create a pointer to a class non-static function to call it by several objects.

Example class:

class A
{
    public:
        int getNumber() { return 5; }
};

I have created a pointer to the class function with the same signature:

int (A::*funcPtr)();

After this, I have initialized it like this:

funcPtr = A::getFive;

When I try to compile, I get the following error:

invalid use of non-static member function 'int A::getNumber()'

What is wrong in my pointer declaration? I tried to make the function const and change the return type, but it didn't help me.


Solution

  • Following would work:

    funcPtr = &A::getNumber;