Re: Requesting sample code!
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Tue, 04 Nov 2008 20:51:51 -0800
On Tue, 4 Nov 2008 20:13:00 -0800, Robby
<Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
snip code that doesn't do what is wanted
But I am looking all over for sample code which embeds the functions or
pointers to function in structures. In other words I am looking for sample
In C, you cannot embed a function in a structure.
A pointer to function is a scalar object conceptually no different
than a pointer to int or pointer to char or pointer to struct. You
have already posted gobs of code in which a member is a pointer.
Putting a pointer to function in a structure is no different.
code which would show how to include funtions or pointers to functions as
members of a struct. Very similar ... you know like we do in C++ when we have
Here is a your pointer to function that was defined at block scope in
the code I snipped above:
int (*pf)(int, int);
Here is some code you posted previously in the thread "static array
question?":
typedef struct i{
short v;
}w;
typedef struct z{
int x;
w *xxx;
}f;
Notice that struct z contains two members, the second being a pointer
to struct i. Adding your function pointer after xxx, we get:
typedef struct z{
int x;
w *xxx;
int (*pf)(int, int);
}f;
struct z now contains three members, the third being a pointer to
function.
Given this simplicity, I have to believe I'm missing the real issue in
your question.
methods in our classes. I want to do the same thing with structures. Is this
possible and can someone direct me to some samples or books which illustrate
on how to code this.
The syntax for declaring an object is the same whether the object is a
member of a structure or "stand-alone". Since you know how to declare
an independent pointer to function, you also know how to declare one
that is a member of a function. (You didn't ask but just for
completeness, you access such a member the same way you access any
structure member, using either the . or -> operator as appropriate.)
I would show an example but I don't even know where to start to code this,
but I know it is possible because I have seen something similar to what I
want to do at:
http://bytes.com/forum/thread583012.html
But I would like a full description of how to implement functions and
pointers to functions as members of a struct which is not posted as a
question but rather as a tutorial or book!
Write 100 times, a function can never be a member of a struct.
--
Remove del for email
.
- Follow-Ups:
- Re: Requesting sample code!
- From: Robby
- Re: Requesting sample code!
- References:
- Requesting sample code!
- From: Robby
- Requesting sample code!
- Prev by Date: Requesting sample code!
- Next by Date: VS2005 conversion of '\n\r' to 0x0D0A
- Previous by thread: Requesting sample code!
- Next by thread: Re: Requesting sample code!
- Index(es):
Relevant Pages
|
Loading