I have this code that defines struct (incoming is simple struct)
#define FUNCS_ARRAY 3
struct func
{
void (AA::*f) (incoming *);
int arg_length;
};
func funcs[FUNCS_ARRAY];
then in class AA body i define the pointer Array like this :
funcs[0] = { &AA::func1, 4 };
funcs[1] = { &AA::func2, 10 };
funcs[2] = { &AA::func2, 4 };
when i try to call one of the functions via the array im getting compilation error:
if i call it like this (p is incoming ):
(*funcs[p->req]->*f)(p);
im getting this error:
error: no match for ‘operator*’ in ‘*((AA*)this)->AA::funcs[((int)p->AA::incoming::req)]’
when i try to call it like this :
(funcs[p->req]->*f)(p);
im getting :
error: ‘f’ was not declared in this scope
when i try this :
(funcs[p->req].f)(p);
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((AA*)this)->AA::funcs[((int)p->AA::incoming::req)].AA::func::f (...)’, e.g. ‘(... ->* ((AA*)this)->AA::funcs[((int)p->AA::incoming::req)].AA::func::f) (...)’
what is the right way to access the function pointer in side the struct ?
To call a member function through a pointer-to-member-function you need that pointer and an instance of the appropriate class.
In your case, the pointer-to-member is
funcs[i].f, and I’ll assume you have an instance ofAAcalledaa. Then you can call that function like this:If
aais a pointer-to-AA, then the syntax would be:If you’re calling from within a (non-static) member function of
AA, then try: