Array of Functions pointing to in Class functions with arduino

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Proos
    New Member
    • May 2013
    • 2

    #1

    Array of Functions pointing to in Class functions with arduino

    Hello all,

    At the moment im trying out with pointing to an array of functions. I got this working as following:
    Code:
    typedef void (* functionPtr) ();
    
    functionPtr functions[2][2]={{do11,do12}, {do21,do22}};
                          
    
    void do11(){DEBUG_PRINTLN("11");}
    void do12(){DEBUG_PRINTLN("12");}
    void do21(){DEBUG_PRINTLN("21");}
    void do22(){DEBUG_PRINTLN("22");}
    
    
    void loop(){
             A=0;
             B=1;
             functions[A][B]();
    }
    But now I'm trying to use this to point to a function inside a class so instead of do11, i want to be able to point to Basic.Do11. Somehow this doesnt work and I keep on getting this message:

    error: argument of type 'void (Basic::)()' does not match 'void (*)()'

    Any ideas?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are stuck here.

    The class member functions must have all arguments the function needs at compile time. One argument that's needed is the address of the object to be used in the function call. Since this is not known at compile time, the compiler secretly adds the this pointer as the first argument. Therefore, each member function has an invisible class* as the first argument.

    But if you get sneaky and add this yourself, your member function will have two class* arguments. So things still won't work for you.

    You can add a class static function but now you don't have access to the class member data because the static function is not a member function.

    This all boils down to advising you that if you need to choose your function by address you are performing polymorphism and for that you need a class hierarchy and virtual functions.

    C++ is all about data protection.

    Otherwise, you will need to switch to C which has no real data protection and will not prevent you from doing anything you want.

    Comment

    • Proos
      New Member
      • May 2013
      • 2

      #3
      Thanks for that information then I quess I just need to think of a different way to do it. Thanks alot.

      Comment

      Working...