0

In A.h:

class A 
{
    typedef void (A::*fp)();
    void test1();
    void test2();
    A(int a);
}

In A.cpp I have:

A::A(int a)
{
    fp fpointers[] = {&A::test1,&A::test2};

    fpointers[a]();
}
void A::test1()
{
//do something
}
void A::test2()
{
//do something
}

This is a reduced and simplified version of what I have. But the problem is still valid. From my humble experience 'fpointers' is how you define and declare an array of function pointers. I am having problem with line fpointers[a]();I am getting this error:

expression preceding parentheses of apparent call must have (pointer-to-) function type

What am I doing wrong?

3
  • @user4581301 Yeah, will fix it. Thanks. Commented Dec 7, 2016 at 18:40
  • Off topic: Take a look at std::function. Bit more runtime overhead, but cleans up some of the messiness of method pointer syntax. Especially interesting with lambda expressions and std::bind. Commented Dec 7, 2016 at 18:44
  • @user4581301 Thanks, will do :-) Commented Dec 7, 2016 at 18:47

1 Answer 1

4

Syntax would be

(this->*fpointers[a])();

as you need instance.

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.