Function Pointers To A Class Method.

Hello!
I do not know how to get the address of a class method. All of the following attempts do not work:

//Method is called "tick." Type: void tick(void);
//Class name: "LCD"

void (*hi)(void)=&tick;          //error: cannot convert 'void (LCD::*)()' to 'void (*)()' in initialization
void (*hi)(void)=&this->tick;    //error: cannot convert 'void (LCD::*)()' to 'void (*)()' in initialization
void (*hi)(void)=&this.tick;     //error: request for member 'tick' in '(LCD*)this', which is of pointer type 'LCD*' (maybe you meant to use '->' ?)
void (*hi)(void)=&LCD::tick;     //error: cannot convert 'void (LCD::*)()' to 'void (*)()' in initialization

(Yes, the class and function are already defined and functional. I want to do this because I would like to have “LCD::tick” run on a timer based interrupt.)

Fuzzyzilla:
Hello!
I do not know how to get the address of a class method. All of the following attempts do not work:

//Method is called "tick." Type: void tick(void);

//Class name: “LCD”

void (hi)(void)=&tick;          //error: cannot convert 'void (LCD::)()’ to ‘void ()()’ in initialization
void (hi)(void)=&this->tick;    //error: cannot convert 'void (LCD::)()’ to 'void (
)()’ in initialization
void (hi)(void)=&this.tick;    //error: request for member ‘tick’ in '(LCD)this’, which is of pointer type ‘LCD*’ (maybe you meant to use ‘->’ ?)
void (hi)(void)=&LCD::tick;    //error: cannot convert 'void (LCD::)()’ to ‘void (*)()’ in initialization



(Yes, the class and function are already defined and functional. I want to do this because I would like to have "LCD::tick" run on a timer based interrupt.)

hmmm… you are trying to run a member function of a specific instance of a class object from an ISR? can you provide some pseudo code?

As far as I know you can't. A method is NOT the same as a function!

Mark

This is a little ugly, but shows how to have a pointer to method:

class Test
{
  public:
    void Test1(char *s);
    void Test2(char *s);
};

void Test::Test1(char *msg)
{
  Serial.print("In Test1, msg = ");
  Serial.println(msg);
}

void  Test::Test2(char *msg)
{
  Serial.print("In Test2, msg = ");
  Serial.println(msg);
}

void setup()
{
  Serial.begin(9600);

  void (Test::*fPtr)(char *s) = &Test::Test1;
  Test myTest;

  (myTest.*fPtr)("2");
  fPtr = &Test::Test2;
  (myTest.*fPtr)("Hello");
  
}

void loop() {}

You might make it more elegant using a typedef.