PC Review


Reply
Thread Tools Rate Thread

basic question about a callback function and delegates

 
 
jmDesktop
Guest
Posts: n/a
 
      30th Jun 2008
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.
 
Reply With Quote
 
 
 
 
AliR \(VC++ MVP\)
Guest
Posts: n/a
 
      30th Jun 2008
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" <(E-Mail Removed)> wrote in message
news:9f987037-7c52-494a-82e0-(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Jeff Louie
Guest
Posts: n/a
 
      30th Jun 2008
>>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 ***
 
Reply With Quote
 
AliR \(VC++ MVP\)
Guest
Posts: n/a
 
      1st Jul 2008
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>>>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 ***



 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      1st Jul 2008
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");
}
}

 
Reply With Quote
 
Jeff Louie
Guest
Posts: n/a
 
      1st Jul 2008
>>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 ***
 
Reply With Quote
 
AliR \(VC++ MVP\)
Guest
Posts: n/a
 
      2nd Jul 2008
That's pretty cool.

AliR.


"Marc Gravell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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");
> }
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Callback function from native C to C++\CLI using non-static member or delegates smmk25@hotmail.com Microsoft VC .NET 6 25th May 2007 05:02 AM
Delegates with non-static callback function =?Utf-8?B?SC5CLg==?= Microsoft VC .NET 4 5th Oct 2005 04:11 PM
PinvokeDLL or delegates or ??? ... Calling C# delegates from C++ as CALLBACK Roland Rehmnert Microsoft Dot NET Compact Framework 1 20th Feb 2005 12:21 PM
Callback function question =?Utf-8?B?bXNjZXJ0aWZpZWQ=?= Microsoft Access Form Coding 1 12th Jan 2005 07:53 PM
CallBack Functions / Delegates Fabiano Maciel Microsoft VB .NET 2 5th Oct 2003 10:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:21 AM.