473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

basic question about a callback function and delegates

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
6 2135
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
>>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
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
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
>>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pawel | last post by:
Hi! I have a .dll library, with some exported function, which looks something like this: extern "C" { int exported_f (int (__cdecl *callback) (int a, int b)) ; } now, I want to invoke this...
4
by: Bob Rock | last post by:
Hello, I was wondering if callback delegates, called in response to a event, are executed in their own thread. I was suspecting the OS might spawn a new thread and have the delegate execute in...
4
by: H.B. | last post by:
Hi, I successfully implement a static callback function for my dll usign delegates. Now, I need to use member function instead of static function. How can I make that (in Managed C++). Hugo
3
by: Glen | last post by:
This is my first attempt as asynchronous processing. I have created a small test app as proof of concept, but I am having one proglem. In the example (code listed below), my callback routine has...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
6
by: Bart Burkhardt | last post by:
Hi, I could use some help in setting a C# callback function that an external unmanaged dll will call on a event. Using a delegate and use the external callback set function doesn't work. The...
2
by: Jon E. Scott | last post by:
I've got a project where I need to create a C# project and a Delphi DLL, in which the DLL has a callback function to send statuses back to the C# application. It seems pretty straightforward in a...
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.