> From: Glynn Clements
> Sent: Tuesday, June 23, 1998 11:39 PM
> Subject: RE: ptrs to functions and void*
>
> Mullen, Patrick wrote:
>
> > There's really not much to either topic.
> > The one hard part about pointers to functions
> > is to remember to put parentesis around the
> > * and the function name.
> >
> > void (*funcpoint)();
> > void func() { printf("Hello\n"); }
>
> Better still, use the ANSI C syntax:
>
> void (*funcpoint)(void);
> ^^^^
> void func(void) { printf("Hello\n"); }
> ^^^^
>
Is my face red. The one time I don't explicitly
put void in the parenthesis... :-)
> > funcpoint = func; /* Notice no parenthesis! */
> > (*funcpoint)(); /* "Hello\n" is displayed */
> >
> > Function pointers must point to static functions.
>
> Not if you mean static in the sense of the C keyword `static', i.e.
> not visible outside of the compilation unit in which they are
> declared. They can point to any function.
>
(I'm glad you said this, because it caused me to finally
get the book and learn it, rather than just fix the
compiler error!)
Static, when referring to a C++ member function, means the
function is global and exists even though no object has been
created. I think the reason a function must be _static_ when
making a pointer to it is if it's not static the location in
memory where the function resides can change. Basically,
it would be too hard for the compiler to handle since there
would have to be code to actually locate the function each
time the value was set.
> > Any function which is standalone (not part of a
> > class; freely defined with global scope) is static.
> > (Others on the list verify this, please.) Member
> > functions (C++) are only static if you give the
> > static keyword. You cannot mix static with the
> > virtual keyword. (Grrr...)
>
> There's no point trying to explain C++ in a few lines. Actually, there
> probably isn't much point in trying to explain C++ in less than 200
> pages.
>
Agreed, and the new section above probably didn't help. ;-)
> > void pointers (vastly different than the
> > case of above. Above was a pointer to a function
> > with void parameters and void return type.
>
> Nope. Above was a pointer to a function with *unspecified* parameters
> and void return type (i.e. K&R style function declaration). If you
> want to specify that the function takes no parameters, you have to
> explicitly specify `void' as the parameter list.
>
By this, do you mean the function could point to
void func1(void); OR
void func2(int, int); OR
void func3(float);
etc. ? Did I accidentally hit on the way to make the same
pointer point to any function with the same return value, but
any parameter list, or am I reading too much into what you're
saying?
[snip of part of void data pointer discussion]
> You can't dereference a void pointer. You can assign a void pointer to
> a typed pointer (and vice-versa) without the need for an explicit
> cast.
>
> > void *pointertoanything;
> > char mychar;
> > int myint;
> > char *mycharptr;
> > int *myintptr;
> >
> > pointertoanything = &mychar;
> > mycharptr = (char*)pointertoanything;
> > pointertoanything = &myint;
> > myintptr = (int*)pointertoanything;
>
> Both of these type casts are optional.
>
You are correct. My example should have been --
void *pointertoanything;
char mychar;
char mychar2;
pointertoanything = &mychar;
mychar2 = *(char*)pointertoanything;
> --
> Glynn Clements <[EMAIL PROTECTED]>
>
>
~Patrick