0

I have a class that contains two similar non-static functions, I want to assign one of them on the fly to a function pointer:

A.h:

class A{
    private:
    void (*funcPtr)(int);
    void do_sth(int i);
    void do_sth_else(int i);
}

A.cpp:

A::A(int i){
    if(i == 1)
        A::funcPtr = A::do_sth; // or &A::do_sth;
    else 
        A::funcPtr = A::do_sth_else; // or &A::do_sth_else;
}

But I get such error:

error: cannot convert 'A::do_sth' from type 'void (A::)(int)' to type 'void (*)(int)'

I read multiple similar issues, But cannot apply their solutions on my problem.

1
  • Considering Arduino's memory limitations, function pointers may be a bit overcomplicated. If the compiler can handle it though, why not use lambdas? Commented Jun 21, 2016 at 15:57

1 Answer 1

1

These are member functions. Either make funcPtr a member pointer by qualifying it with class name, or make do_sth(), do_sth_else() static member functions. I'd suggest using & in front of function name when taking the address of it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.