Callback functions

Discussion in 'C++' started by Pratik, Jun 15, 2004.

  1. Pratik

    Pratik Guest

    what are callback functions? Where we require callback functions? In
    what scenario we require callback functions?
     
    Pratik, Jun 15, 2004
    #1
    1. Advertising

  2. Pratik

    Phlip Guest

    Pratik wrote:

    > what are callback functions?


    I send you a letter. Inside is a self-addressed stamped envelop. You put
    something inside that, and drop it in the mail. It comes back to me.

    > Where we require callback functions?


    Callbacks are the simplest form of the Observer Pattern from the book
    /Design Patterns/. Google for those.

    > In what scenario we require callback functions?


    When another module must trigger behavior in my module, without I know when
    the trigger will happen.

    In an event driven architecture, such as a GUI, code paints a window and
    then awaits user events. The user clicks on controls, and the window's event
    dispatcher calls functions assigned to those controls. Some are functions
    that you wrote, providing the application behavior behind your window.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jun 15, 2004
    #2
    1. Advertising

  3. Pratik

    JKop Guest

    Pratik posted:

    > what are callback functions? Where we require callback functions? In
    > what scenario we require callback functions?


    If only one program is running on a system, there's no requirement.


    But consider a system where two or more programs are running.

    Program 1 is the main program. It controls the user interface on the front
    of the safe and it controls the open/close mechanism for the door.

    Program 2 controls the lock mechanism on the safe, nothing more.


    Both programs are running.

    Someone comes over to the safe to open it and take cash out. Program 1 must
    communicate with Program 2 to tell it to unlock the safe. To do such a
    thing, it needs the address of a function.


    Here's what Program 2 looks like:


    void Unlock(void)
    {
    //blah
    }


    Program 1 will want something like the following in it:

    int main(void)
    {
    void (*Unlock)(void);

    Unlock = GetAddressOfUnlockFunctionInProgram2();

    Unlock();
    }


    -JKop
     
    JKop, Jun 15, 2004
    #3
  4. JKop wrote:
    >
    > Pratik posted:
    >
    > > what are callback functions? Where we require callback functions? In
    > > what scenario we require callback functions?

    >

    [snip]

    You are talking about interprocess communication or remote
    procedure calls.

    > If only one program is running on a system, there's no requirement.


    qsort() needs a callback function.


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 15, 2004
    #4
  5. Pratik

    red floyd Guest

    Pratik wrote:

    > what are callback functions? Where we require callback functions? In
    > what scenario we require callback functions?


    Sounds like a homework question.
     
    red floyd, Jun 15, 2004
    #5
  6. Pratik wrote:

    > what are callback functions? Where we require callback functions?
    > In what scenario we require callback functions?


    #include <stdlib.h>

    void qsort(void *base, size_t nmemb, size_t size,
    int(*compar)(const void*, const void*));

    compar(const void*, const void*) is a callback function.

    #include <signal.h>

    typedef void (*sighandler_t)(int);

    sighandler_t signal(int signum, sighandler_t handler);

    handler(int) is a callback function.

    #include <stdio.h>

    typedef struct X {
    int i;
    void (*f)(const X*);
    } X;

    void f(const X* p) {
    fprintf(stdout, "%d = p->i\n", p->i);
    }

    void g(const X*p) {
    p->f(p);
    }

    int main(int argc, char* argv[]) {
    const
    X x = {13, f};
    g(&x);
    return 0;
    }

    f(const X*) is a callback function.

    #include <iostream>

    class X {
    private:
    int I;
    public:
    virtual
    void g(void) {
    std::cout << I << " = I" << std::endl;
    }
    explicit
    X(int i): I(i) { }
    };

    int main(int argc, char* argv[]) {
    X x(13);
    x.g();
    return 0;
    }

    X::g(void) calls the corresponding callback function
    from the virtual function table.
     
    E. Robert Tisdale, Jun 15, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. exquisitus
    Replies:
    23
    Views:
    47,579
    Andrew McDonagh
    Feb 28, 2005
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,610
    Victor Bazarov
    Jun 21, 2004
  3. cadull

    Member Callback Functions

    cadull, May 6, 2005, in forum: C++
    Replies:
    1
    Views:
    488
    cadull
    May 6, 2005
  4. Replies:
    11
    Views:
    968
    Richard Herring
    Jun 7, 2005
  5. Replies:
    1
    Views:
    376
Loading...

Share This Page