function pointer array

I have a question on how to store methods from multiply other class in an array.
All this code is for an arduino controller. The AP_ATO class is instanced as myATO in its .cpp file.

In the .h file,
1
2
3
4
5
6
7
8
#include <AP_ATO.h>

typedef void (*inputHandler)();

class AP_expandedInput{
  ...
  inputHandler callback[8];
  ...


And then in the .cpp file,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void AP_expandedInput::runtime(){
  if (EIN_IRQ == LOW){
    uint8_t i;
		
    uint8_t* inputReg = expIn.inputArray();
		
    for (i=0 ; i<NUM_IO_PORTS ; i++){
      if (inputReg[i] == actionState[i]){
        (*callback[i])();
      }
    }
  }
}

void AP_expandedInput::addATOlowLevelSensor(uint8_t port){
  actionState[port] = HIGH;
  callback[port] = myATO::lowLevelReached;
}


I'm not really sure how I'm supposed to syntax that method from the AP_ATO class.

1
2
3
callback[port] = myATO::lowLevelReached;
//or
callback[port] = myATO.lowLevelReached;
Last edited on
closed account (zb0S216C)
thejosheb wrote:
1
2
3
callback[port] = myATO::lowLevelReached;
//or
callback[port] = myATO.lowLevelReached;

The type of "inputHandler" cannot take the address of a member-function. Instead, you must specify the class to which the function belongs during "inputHandler's"" declaration, like so:

 
typedef void( SomeClass:: *InputHander )( );

And then when creating a pointer, you must specify a function's address:

 
InputHandler NewHandler( &SomeClass::Function );

Calling a member-function through a function-pointer is a little strange:

 
( SomeClassInstance.*NewHandler )( );

Wazzak
Last edited on
so basically the array has to contain member-functions from only one class.
closed account (zb0S216C)
Yes. A pointer to a member-function can only reference instances of the class to which its bound to. In my above examples, "NewHandler" can only call the "SomeClass::Function" function of all instances of "SomeClass", unless you redirect the pointer to another member-function of the same class that matches the specification set by the function-pointer.

Wazzak
Last edited on
Alright thanks for the help.
Topic archived. No new replies allowed.