Talk:Function pointer

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Type safety in older languages?[edit]

Function pointers in C and C++ are both type-safe. How are they any less type-safe than, say delegates in C#? I keep hearing this untrue statement. — Preceding unsigned comment added by 130.76.96.111 (talk) 19:13, 6 September 2013 (UTC)

Object-oriented equivalent?[edit]

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)

Not using function pointers[edit]

"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)

Procedural parameter[edit]

Procedural parameter seems to have a lot in common with this article. If someone agrees, should these two articles mention each other, or maybe even be merged? --Abdull (talk) 11:58, 26 July 2008 (UTC)

Java[edit]

Java's lack of function pointers should be noted/explained. Fig (talk) 14:01, 30 September 2013 (UTC)

Technically, any language supporting abstract interfaces has the same capabilities as function pointers. Using an interface containing a single method effectively makes that a typed function pointer. It's not an exact capability, though, because you can't pass just any (type-matching) method as a parameter to a method expecting a method pointer, or assign it to a method pointer; any such method you use as a method pointer must be defined as implementing the method of the abstract interface, and an actual object must be associated with the method being referenced. But the ability to pass or store a reference to a properly defined method (which is just a function pointer deep down) still exists in these languages. This might be one reason that delegates were never added to Java by its designers, since some programmers consider them unnecessary syntactic sugar for an already-existing feature. — Loadmaster (talk) 17:55, 30 September 2013 (UTC)

External links modified[edit]

Hello fellow Wikipedians,

I have just modified one external link on Function pointer. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:

When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.

You may set the |checked=, on this template, to true or failed to let other editors know you reviewed the change. If you find any errors, please use the tools below to fix them or call an editor by setting |needhelp= to your help request.

  • If you have discovered URLs which were erroneously considered dead by the bot, you can report them with this tool.
  • If you found an error with any archives or the URLs themselves, you can fix them with this tool.

If you are unable to use these tools, you may set |needhelp=<your help request> on this template to request help from an experienced user. Please include details about your problem, to help other editors.

Cheers.—InternetArchiveBot (Report bug) 21:49, 8 October 2017 (UTC)