Sign in to follow this  
armond

Pointer to function

Recommended Posts

Hi, Could anyone please give me an explaining(including examples) about pointer to function? And how does it differ from function pointers(I believe they are two different things)? I was keeping track of topic about this but occasionally lost track of it... I even remember putting it on my bookmark... But it's gone... Weird... Well thanks anyway... :D

Share this post


Link to post
Share on other sites
A function pointer (believe it or not) is a pointer TO a function.

Any pointer is simply an address to something. Sometimes it points to data, such as integer pointer or character pointers. But sometimes it points to code.

C++ function pointer syntax is messy. Try to avoid it as much as you can.

bool (*equals(void*, void*)) = NULL;

declares a function pointer with the name "equals" to a function which returns a "bool" and takes two "void*" as arguments.

Most people, I think, tend to typedef common function pointer types. I'm not 100% sure on the syntax off the top of my head, so I won't give an example.

Function pointers aren't very useful in C and C++. What's more common (and hundreds of times easier to understand and use) are virtual class methods. A virtual method is simply a function pointer that is assigned implicitly (by the compiler) when an object's constructor is called.

In Java's model for event-driven programming, you pass "function pointers" to the AWT by using EventListener objects. By overloading various "listener" virtual methods, you can let Java hook into your code.

C#'s model is even cooler. In C#, there is a special type of class called a delegate, which is simply a type-safe function pointer with pretty syntax.

So yeah. Bottom line: most of the time, you use virtual methods. Function pointers are archaic.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tac-Tics
A function pointer (believe it or not) is a pointer TO a function.

Any pointer is simply an address to something. Sometimes it points to data, such as integer pointer or character pointers. But sometimes it points to code.

C++ function pointer syntax is messy. Try to avoid it as much as you can.

bool (*equals(void*, void*)) = NULL;

declares a function pointer with the name "equals" to a function which returns a "bool" and takes two "void*" as arguments.

Most people, I think, tend to typedef common function pointer types. I'm not 100% sure on the syntax off the top of my head, so I won't give an example.

Function pointers aren't very useful in C and C++. What's more common (and hundreds of times easier to understand and use) are virtual class methods. A virtual method is simply a function pointer that is assigned implicitly (by the compiler) when an object's constructor is called.

In Java's model for event-driven programming, you pass "function pointers" to the AWT by using EventListener objects. By overloading various "listener" virtual methods, you can let Java hook into your code.

C#'s model is even cooler. In C#, there is a special type of class called a delegate, which is simply a type-safe function pointer with pretty syntax.

So yeah. Bottom line: most of the time, you use virtual methods. Function pointers are archaic.


Well, in functional languages (like Python, Haskell, Scheme, Common Lisp, etc) it usually looks a lot nicer (they usually aren't referred to as function pointers, but they are probably implemented with them internally). But, yeah, you should try to use functors in C++ rather than actual function pointers.

Share this post


Link to post
Share on other sites
There is nothing wrong with function pointers, they are nice and flexible.
Say you had a class for a button, you could just include a function pointer to handle when a button was pressed. If you wanted the function to be called when ever the button was pressed you would just call the pointer to the function with the usual parameters in the button handler code.
In C++, if you used virtual methods, for every button that would call a different function you would have to derive a new button class.
With a function pointer for every instance you just set up a different pointer. Saves loads of pointless classes.
Please correct me if there is an easier way around this in C++.

I agree though, C# is much better again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this