Talk:Function pointer
From Wikipedia, the free encyclopedia
[edit] Object-oriented equivalent?
Perhaps there could be something here showing how the same thing can be achieved using Object Oriented Programming? Ubermonkey 20:35, 2 Apr 2005 (UTC)
Also a nice implementation of this using a class where the method is passed as a function pointer (so a method pointer i guess). I've been trying to find an example of this everywhere and i can't find it. -Sharth --128.175.13.43 17:54, 26 October 2005 (UTC)
It would also be nice if the program actually worked.. say.. if it had a definition for the "list" or "listnode" types. Fresheneesz 07:56, 5 June 2006 (UTC)
- One way to do the equivalent of function pointers in Java is to define an interface with a single method. Then one or more classes can implement the interface, and objects of these class types can be passed to other methods. These interface references act something like method pointers:
public interface Foo { public abstract int perf(int a); } class MyFoo implements Foo { public int perf(int a) { return (2*a + 3); } } public class Test { static Foo s_func; public static void main(String[] args) { int x; setFunc(new MyFoo()); x = callFunc(10); } void setFunc(Foo f) { s_func = f; } int callFunc(int a) { return s_func.perf(a); } }
- — Loadmaster 01:00, 9 March 2007 (UTC)
[edit] Not using function pointers
"Imagine rewriting the above code without function pointers. The fsum() and fproduct() function would both require loops iterating through the linked list." Isn't it contradicting to say that when the code does exactly that--or am I missing something? Guardian653 03:55, 2 December 2006 (UTC)
- Agreed. Seems like the author is not NPOV since he calls it a "clearly" "powerful" mechanism. Using it to obfuscate code is arguably not the best application of this feature. 24.6.99.30 12:50, 3 January 2007 (UTC)