-1
#include <iostream>
#include <stdio.h>
#include <stdint.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

 
class FooBar{
public:
        typedef void(FooBar::*OnDio)(void);

    void OnDio0Irq( void ){
        printf("dio0\n");
    };
    void OnDio1Irq( void ){
        printf("dio1\n");
    };
    FooBar(){
        OnDio  dioArray[] = {&OnDio0Irq, &OnDio1Irq};
        
    };
    
    OnDio  *dioArray[2];
private:
            
};
 
int main(int argc, char* argv[]){
        typedef void(FooBar::*OnDio)(void);
        void (FooBar::*foo)(void);
    OnDio *myPtr;
    FooBar *fb = new FooBar();
    
    myPtr = *(&fb->dioArray[0]);
    foo = (OnDio &)(myPtr[0]);
    (foo)();//me need call fb->dioArray[0]()
    (*myPtr)(); // ?
 }

How can I call a function from an array? In my code i have error:

[Error] must use '.' or '->' to call pointer-to-member function in 'foo (...)', e.g. '(... ->* foo) (...)'

[Error] must use '.' or '->' to call pointer-to-member function in '* myPtr (...)', e.g. '(... ->* * myPtr) (...)'

1

2 Answers 2

1

To call a pointer to member function (ptmf), you need an instance and the ptmf, together.

OnDio is already typedef'ed to be a pointer type, so you may not want OnDio pointers.

Also, you initialize a local temporary in the constructor, not the dioArray of "this" instance.

This answer is also helpful: C++: Array of member function pointers to different functions

Here is your code, corrected to call dio0 through a pointer to member function.

#include <iostream>
#include <stdio.h>
#include <stdint.h>

class FooBar {
public:
    typedef void(FooBar::*OnDio)(void);

    void OnDio0Irq(void) {
        printf("dio0\n");
    };
    void OnDio1Irq(void) {
        printf("dio1\n");
    };
    FooBar() {
        // declaring a local OnDio array just masks the actual member and then it gets tossed
        // need to initialize this instance, not some local temporary
        dioArray[0] = &FooBar::OnDio0Irq;
        dioArray[1] = &FooBar::OnDio1Irq;
    };

    OnDio dioArray[2];
private:

};

int main(int argc, char* argv[]) {
    // need instance
    FooBar fb;
    // need pointer to member function
    FooBar::OnDio func = fb.dioArray[0];
    // call pointer to member function using instance
    (fb.*func)();
}
Sign up to request clarification or add additional context in comments.

Comments

0
#include <iostream>
#include <stdio.h>
#include <stdint.h>



class FooBar {
public:
    typedef void(FooBar::*OnDio)(void);
    OnDio dioArray[2];
    void OnDio0Irq(void) {
        printf("dio0\n");
    };
    void OnDio1Irq(void) {
        printf("dio1\n");
    };
    FooBar() {
        dioArray[0] = &FooBar::OnDio0Irq;
        dioArray[1] = &FooBar::OnDio1Irq;
    };


private:

};

int main(int argc, char* argv[]) {

    FooBar* fb  = new FooBar();
    for (int i = 0; i < sizeof(fb->dioArray) / sizeof(fb->dioArray[0]); i++)
    {
        (fb->*fb->dioArray[i])();
    }

}

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.