By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

How to declare pointer to functions in structures?

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mackan wrote:
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
Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.