简体   繁体   中英

Passing function pointer from Arduino to C++

This question is about C++, but it interact with Arduino, and since I am not a software developer, I would be happy to get some advice.

In Arduino, a user creates an instance of a class and set its delegate method:

Arduino main program:

    //instance
    Main interpreter;
    //set delegate
      interpreter.setDelegate(intepreterDelegate);
    //delegate function
         float intepreterDelegate(char *arg){return 3;}

Main class, is then creating an instance of another class, called Sub , and so :

Sub - > send delegate to Main-> send delegate to Arduino

The Main class does successfully get the delegate message from the Sub with:

Main.h

  //being called from the sub
   static float fromSubDelegate(char *arg){

        // *Here I am trying to push forward this delegate out to the user on arduino
        Main. b;   
        float result = (b.*b.fpAction)(arg);
        return  result;


     };

    float (Main.::*fpAction)(char*) = 0 ;
    void setDelegate( float(*fp)(char*));

The problem is here- on Main.cpp where I set the delegate

//arduino set this delegate of the main class .cpp
void Main.::setDelegate( float(*fp)(char*))
{

    fpAction = fp; // *gives error because fpAction is a member function

}

I have provided all the data I have. I am not a C++ programmer, hence I might be doing something wrong.

This isn't really what you asked in the other question. you have a mismatch between what you (seem to) do in the adruino code and what you want in the C++ code.

you cannot change your delegate from a non-member function to a member-function and you define a member function (ie the delegate) outside of the class that it uses.

what you're trying doesn't make sense currently. your arduino defines an instance

Main interpreter;

it then defines and sets the delegate (ok so far) IN the interpreter instance.

but in fromSubDelegate() you create a new instance b. whichever delegate you may have set in the interpreter instance won't be set in the b instance, unless the function pointer is static, but in that case you don't even need the fromSubDelegate() function, you can just call the function pointer directly.

You seem to have a wrong idea of how things work. The thing to take home is that if you have a non-static member function of a class, there is a hidden extra parameter, which you can use from within the function as 'this'. so a member function you define as

class X
{
    void func(int i);
}

isn't the same as

void func (int i);

defined outside of a class. It is really more similar to void

func(X* this, int i); 

although that's semantically 'correct', it isn't syntactically.

Thanks to the help of oreubens , I found a solution - just to pass directly from the main to the sub, a pointer for the function , so :

void Main::setDelegate( float(*fp)(char*))
{
    fpAction=fp;
    sub->setDelegate(fpAction);



}

works great. thanks again .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM