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"); }
          ^^^^

> 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.

> 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.

> 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.

> A void pointer can point to *anything*,

Any *data* pointer can be cast to a void pointer and back again to
obtain the original value. Whether or not you can cast a *function*
pointer to a void pointer and back again is implementation dependent
(although I can't cite a specific architecture where you can't).

> but you must type cast it if it is on the right side of an equals
> sign.

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.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to