basic question about a callback function and delegates

J

jmDesktop

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.
 
A

AliR \(VC++ MVP\)

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.
 
A

AliR \(VC++ MVP\)

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.
 
M

Marc Gravell

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");
}
}
 
J

Jeff Louie

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/function-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.

If I have not answered this ?, please clarify.

Regards,
Jeff
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top