How to declare pointer to functions in structures?

M

Mackan

Hi!

I'm trying to declare a structure that will be including pointers to
functions. These structures in turn will be pointed to in an array and
depending on a index value that specific function in the structure pointed
to by the index will be called with various data.

Say i have a couple of functions:

int func1(int , int);
int func2(int, int);

Then i have a structure:

typedef struct {
int someval;
/* Below is where i'm not sure how to declare,
* here is where one of the functions above will be inserted */
int *somefunction();
} mystruct;

Then i initialize the array wich should point to structures:

mystruct myarray[] = {{10,func1()},{20,func2()},{30,func1()}};

Now when executing the line below and say array_index below is 0 it will
call function func1():

myarray[array_index]->somefunction(value1, value2);

Anybody understand what i mean? I just don't know how to declare the
function in the structure "mystruct" above.

Mats
 
V

Victor Bazarov

Mackan said:
I'm trying to declare a structure that will be including pointers to
functions. These structures in turn will be pointed to in an array and
depending on a index value that specific function in the structure pointed
to by the index will be called with various data.

Say i have a couple of functions:

int func1(int , int);
int func2(int, int);

Then i have a structure:

typedef struct {
int someval;
/* Below is where i'm not sure how to declare,
* here is where one of the functions above will be inserted */
int *somefunction();

Should be

int (*somefunction)(int,int);
} mystruct;

Then i initialize the array wich should point to structures:

mystruct myarray[] = {{10,func1()},{20,func2()},{30,func1()}};

Now when executing the line below and say array_index below is 0 it will
call function func1():

myarray[array_index]->somefunction(value1, value2);

Anybody understand what i mean? I just don't know how to declare the
function in the structure "mystruct" above.


V
 
P

pete_k_1976

Your declaration of 'somefunction' in 'mystruct' reads 'somefunction'
is a function taking no arguments and returning type pointer to int.
Because of operator precedence in C++ you'll need parentheses so the
declaration should be

int (*somefunction)();

which reads 'somefunction' is a pointer to a function taking no
arguments and returning type int, this declaration is very different
than what you were doing initially and is probably what you want.

Just something to keep in mind, have you considered using pure abstract
classes instead of pointers to functions? Often times (not always) they
lead to a more elegant design.

- pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top