ryan
ryan

Reputation: 21

What is the syntax for calling a member function pointer that is a member of a structure array in C++

This question is similar to what I'm trying to do Calling C++ member function pointer from a struct . However my structure contains a member function pointer that is defined in a different class then the one the structure is defined and used in. Here is some example code of how my classes, structures and function pointers are laid out.

// Alpha.h:
class Alpha{
public:
    void function1(char name[], int number);
    void function2(char name[], int number);
    void function3(char name[], int number);

    typedef void (Alpha::*My_func_ptr)(char name[], int number);

    static My_func_ptr functionTable[];
};

// Alpha.cpp:
#include "Alpha.h"

Alpha::My_func_ptr Alpha::functionTable[] = {
    &Alpha::function1, 
    &Alpha::function2, 
    &Alpha::function3
};

void Alpha::function1(char name[], int number)
{
    //some stuff
}

void Alpha::function2(char name[], int number)
{
    //some stuff
}

void Alpha::function3(char name[], int number)
{
    //some stuff
}

// Beta.h:
#include "Alpha.h"

typdef struct{
    char bName[10];
    Alpha::My_func_ptr fptr;
}ptr_structure;

class Beta{
public:
      void betafunction();

      Alpha alphaobject;
      ptr_structure str_array[3];
};

// Beta.cpp:
#include "Beta.h"

void betafunction()
{
    str_array[0].fptr = alphaobject.functionTable[0];
    str_array[1].fptr = alphaobject.functionTable[1];
    str_array[2].fptr = alphaobject.functionTable[2];

    (str_array[0].fptr)("name", 1); //gives error expression must have 
                                    //(pointer-to-)  function type

    (this->*str_array[0].fptr)("name", 1);
    //error pointer-to-member selection class types are incompatible "Beta" and "Alpha"

    //sample function pointer call using function table from other class,
    //this syntax compiles and runs without error.
    (alphaobject.*Alpha::functionTable[0]("name", 1); 
}

As you can see I can call the function pointer from an array, but can't seem to figure out how to call a function pointer from inside an array of structures.

Upvotes: 1

Views: 1497

Answers (3)

You can go to one of two solutions, depending on how readable you want the code. The unreadable version (which might even be wrong, and I won't even try to compile):

void Beta::betafunction() {
   Alpha a;
   (a.*(strArray[0].fptr))("name",1);
}

But I would actually try to make things a bit simpler:

void Beta::betafunction() {
   Alpha a;
   Alpha::My_func_ptr mptr = strArray[0].fptr;
   (a.*mptr)("name",1);
}

I believe the second to be much more readable, and the compiler can optimize away mptr pretty easily, so there is no point in trying to play guru with the syntax.

Upvotes: 0

Michael Burr
Michael Burr

Reputation: 340198

When calling a through member function pointer, you need to have an instance of the object associated with that pointer:

 (alphaobject.*(str_array[0].fptr))("name", 1)
  ^^^^^^^^^^^

Upvotes: 3

Kaz
Kaz

Reputation: 58578

I would think:

(object.*functionTable[0])(args, ...);
(objptr->*functionTable[0])(args, ....);

IIRC, the combination of object and the .* operator is like a big unary operator. So that has lower precedence to the [0] postfix. However, it also has lower prededence than the function call postfix operator (args, ...)

Analogy:

(*foo)();  /* classic C */

Of course the * operator is not required when calling a regular function. But if you do write it, you need the parens, because *foo() means something else.

Upvotes: 0

Related Questions