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.

Makan
  • 2,508
  • 4
  • 24
  • 39
  • Considering Arduino's memory limitations, function pointers may be a bit overcomplicated. If the compiler can handle it though, why not use [lambdas](http://stackoverflow.com/questions/24352785/c-closure-to-pass-member-function-as-normal-function-pointer)? – Panagiotis Kanavos Jun 21 '16 at 15:57

1 Answers1

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.

lorro
  • 10,687
  • 23
  • 36