basic question about a callback function and delegates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jmDesktop

    basic question about a callback function and delegates

    In a function that takes another function (function pointer) as a
    argument, or the callback function, which is the one that "calls
    back"? I'm having a hard time understanding the language.

    Am I right that if function A is:

    function A(*function pointer to B aka callback func, other arguments)
    {
    call (B); //calls
    }

    callback function:

    function B(arguments) //the callback function
    {
    return something to the caller; \\calls the caller back when done;
    and is why it is called the "callback"
    }

    Then When A calls B inside of A's implementation that when B is done
    with what ever it "calls back" A, sending it its return result? the
    callback is the one that calls back. I know that sounds painfully
    obvious, but wanted to be sure I understood it.

    Is a Delegate the same thing in C#? Is a Delegate in C# a callback,
    like above (assuming I was right.) Also, just curious, does OOP
    polymorphism take over for callbacks?

    Thank you.
  • AliR \(VC++ MVP\)

    #2
    Re: basic question about a callback function and delegates

    Callback function is the one that gets called from the original function.
    The fact that the called function returns a value or not is irrelevant.

    So in your case function B is a callback function that gets called from
    function A.

    C# delegates as far as I know serve the same purpose as function pointers in
    c++. function pointers are what makes declaring callback functions
    possible.

    There is no polymorphism involvement when it comes to function pointers.

    AliR.


    "jmDesktop" <needin4mation@ gmail.comwrote in message
    news:9f987037-7c52-494a-82e0-d1d693dc33f6@f6 3g2000hsf.googl egroups.com...
    In a function that takes another function (function pointer) as a
    argument, or the callback function, which is the one that "calls
    back"? I'm having a hard time understanding the language.
    >
    Am I right that if function A is:
    >
    function A(*function pointer to B aka callback func, other arguments)
    {
    call (B); //calls
    }
    >
    callback function:
    >
    function B(arguments) //the callback function
    {
    return something to the caller; \\calls the caller back when done;
    and is why it is called the "callback"
    }
    >
    Then When A calls B inside of A's implementation that when B is done
    with what ever it "calls back" A, sending it its return result? the
    callback is the one that calls back. I know that sounds painfully
    obvious, but wanted to be sure I understood it.
    >
    Is a Delegate the same thing in C#? Is a Delegate in C# a callback,
    like above (assuming I was right.) Also, just curious, does OOP
    polymorphism take over for callbacks?
    >
    Thank you.

    Comment

    • Jeff Louie

      #3
      Re: basic question about a callback function and delegates

      >>There is no polymorphism involvement when it comes to function
      pointers<<

      Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


      Regards,
      Jeff

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • AliR \(VC++ MVP\)

        #4
        Re: basic question about a callback function and delegates

        Yes I know that you can achieve polymorphism using function pointers. As a
        matter of fact the vtable is made up function pointers. But once you assign
        a function to a function pointer that is the only function that will get
        called. So in the OP's original message whatever is assigned to B is what
        is going to get called, it is not going to called a derived classes version
        if it is overwritten in a derived class.


        AliR.


        "Jeff Louie" <anonymous@devd ex.comwrote in message
        news:eTRsQyv2IH A.2064@TK2MSFTN GP02.phx.gbl...
        >>>There is no polymorphism involvement when it comes to function
        pointers<<
        >
        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

        >
        Regards,
        Jeff
        >
        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Marc Gravell

          #5
          Re: basic question about a callback function and delegates

          I know you mention function pointers, but I'll limit my reply to
          delegates; with the comment "serve the same purpose as function pointers
          in c++" I'll assume we're talking about the same thing...

          A regular .NET delegate to an intance method actually includes the
          instance in the delegate - so it is a non-question to discuss derived
          classes once you have the delegate. But note that polymorphism is still
          respected; see below for an example.

          Marc

          using System;
          static class Program {
          static void Main() {
          Foo foo = new Bar();
          Action act = foo.Test; // get delegate
          act(); // invoke
          }
          }
          class Foo {
          public virtual void Test() {
          Console.WriteLi ne("Foo");
          }
          }
          class Bar : Foo {
          public override void Test() {
          Console.WriteLi ne("Bar");
          }
          }

          Comment

          • Jeff Louie

            #6
            Re: basic question about a callback function and delegates

            >>Is a Delegate in C# a callback<<

            JM.. You can use a delegate in C# as a callback. The delegate in C# can
            encapsulate behavior as can a function pointer in C++.

            Here is an article that discusses the use of function pointers as a
            callback.



            Note that function pointers can encapsulate behavior.

            "A function pointer is a variable that stores the address of a function
            that can
            later be called through that function pointer. This is useful because
            functions
            encapsulate behavior."

            Delegates are similar to C++ function pointers and can encapsulate
            behavior.

            MSDN "A delegate is a type that safely encapsulates a method, similar to
            a
            function pointer in C and C++. Unlike C function pointers, delegates are
            object-oriented, type safe, and secure."

            MSDN "An interface reference or a delegate can be used by an object with
            no
            knowledge of the class that implements the interface or delegate
            method."

            So both function pointers and delegates can encapsulate behavior.
            >>Also, just curious, does OOP polymorphism take over for callbacks?<<
            If I have not answered this ?, please clarify.

            Regards,
            Jeff

            *** Sent via Developersdex http://www.developersdex.com ***

            Comment

            • AliR \(VC++ MVP\)

              #7
              Re: basic question about a callback function and delegates

              That's pretty cool. :)

              AliR.


              "Marc Gravell" <marc.gravell@g mail.comwrote in message
              news:OgXI7t42IH A.4928@TK2MSFTN GP04.phx.gbl...
              >I know you mention function pointers, but I'll limit my reply to delegates;
              >with the comment "serve the same purpose as function pointers in c++" I'll
              >assume we're talking about the same thing...
              >
              A regular .NET delegate to an intance method actually includes the
              instance in the delegate - so it is a non-question to discuss derived
              classes once you have the delegate. But note that polymorphism is still
              respected; see below for an example.
              >
              Marc
              >
              using System;
              static class Program {
              static void Main() {
              Foo foo = new Bar();
              Action act = foo.Test; // get delegate
              act(); // invoke
              }
              }
              class Foo {
              public virtual void Test() {
              Console.WriteLi ne("Foo");
              }
              }
              class Bar : Foo {
              public override void Test() {
              Console.WriteLi ne("Bar");
              }
              }
              >

              Comment

              Working...