By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

basic question about a callback function and delegates

P: n/a
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.
Jun 30 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <ne***********@gmail.comwrote in message
news:9f**********************************@f63g2000 hsf.googlegroups.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.

Jun 30 '08 #2

P: n/a
>>There is no polymorphism involvement when it comes to function
pointers<<

http://www.geocities.com/jeff_louie/OOP/oop37.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Jun 30 '08 #3

P: n/a
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" <an*******@devdex.comwrote in message
news:eT**************@TK2MSFTNGP02.phx.gbl...
>>>There is no polymorphism involvement when it comes to function
pointers<<

http://www.geocities.com/jeff_louie/OOP/oop37.htm

Regards,
Jeff

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

Jul 1 '08 #4

P: n/a
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.WriteLine("Foo");
}
}
class Bar : Foo {
public override void Test() {
Console.WriteLine("Bar");
}
}

Jul 1 '08 #5

P: n/a
>>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.

http://www.cprogramming.com/tutorial...-pointers.html

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 ***
Jul 1 '08 #6

P: n/a
That's pretty cool. :)

AliR.
"Marc Gravell" <ma**********@gmail.comwrote in message
news:Og**************@TK2MSFTNGP04.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.WriteLine("Foo");
}
}
class Bar : Foo {
public override void Test() {
Console.WriteLine("Bar");
}
}

Jul 2 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.