0

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.

1
  • 5
    You mean funcPtr = &A::getNumber;? Commented May 4, 2020 at 6:50

1 Answer 1

5

Following would work:

funcPtr = &A::getNumber;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.