Advertisement

Pointer to a function?

Started by July 14, 2001 03:18 PM
3 comments, last by bose 23 years, 7 months ago
How do you create a pointer to a function? I think I saw this explained once but I can''t find where it was
I asked some stuff about function pointers a few days ago. Here is a link to the posts.
Click me Somebody said how to make them in there.



Drakonite

[Insert Witty Signature Here]
Shoot Pixels Not People
Advertisement


This way:

RETURN_TYPE (FUNCTION_NAME)(PARAMETERS_TYPE)


Example:

int Foo(int i);
int Foo2(int i);


int (pFunc)(int);

pFunc=Foo;

pFunc(69);//Execute Foo()

pFunc=Foo2;

pFunc(666);//Execute Foo2



What the hells!
What the hells!
Great, that helps a lot.

Another question though...

How would I pass that pointer to a function?
What I am trying to do is pass a pointer to a function - to a function like this:

Create(WINDOW, etc, etc, etc, POINTER_TO_A_FUNCTION)
{
variable_to_receive_function_pointer = POINTER_TO_A_FUNCTION;
// etc
}

This way I can have a class store the pointer to this function and call that function whenever I need it.

Can anyone give a straight example of how I would denotate that? The function will ALWAYS return void and will ALWAYS have no parameters
The easiest way:

  typedef void (*function_ptr)(void);create(... , function_ptr fp, ...){  variable_to_receive_ptr = fp;  // call function:  variable_to_receive_ptr();}  

This topic is closed to new replies.

Advertisement