Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Callback functions

Reply
Thread Tools

Callback functions

 
 
Pratik
Guest
Posts: n/a
 
      06-15-2004
what are callback functions? Where we require callback functions? In
what scenario we require callback functions?
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      06-15-2004
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/bi...UserInterfaces



 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      06-15-2004
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
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-15-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      06-15-2004
Pratik wrote:

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


Sounds like a homework question.
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-15-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ctypes: Setting callback functions in C libraries sjdevnull@yahoo.com Python 1 01-29-2007 11:07 PM
Callback functions problem ajay.sonawane@gmail.com C++ 11 06-07-2005 04:41 PM
Member Callback Functions cadull C++ 1 05-06-2005 01:51 PM
Function pointers (Callback functions) in Java ? exquisitus Java 23 02-28-2005 07:45 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments