Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Callback functions problem

 
 
ajay.sonawane@gmail.com
Guest
Posts: n/a
 
      06-06-2005
Hello ther
I read somewhere that callback function should be global or static
member function.
1: I could use static member functions but how could I access members
of class which ar not static.
2: I could use global function but want to access members of class.
Let me know the possible solutions.

Regards,
Ajay Sonawane

 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      06-06-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello ther
> I read somewhere that callback function should be global or static
> member function.
> 1: I could use static member functions but how could I access members
> of class which ar not static.
> 2: I could use global function but want to access members of class.
> Let me know the possible solutions.
>
> Regards,
> Ajay Sonawane
>

Most callback functions that I've worked with provide a void* pointer, often
called userData or something similar, which is there for you to use as you
see fit. What I do is write a global callback function, and put in that
void* the "this" pointer of the object which wants to receive the callbacks.
Then, in the callback, I simply cast the void* pointer to a pointer to my
class, and use it to call a member function on that object to do the actual
work of the callback.

-Howard



 
Reply With Quote
 
 
 
 
kloodge
Guest
Posts: n/a
 
      06-06-2005
You may try using a friend function. This would give you a global function
that can also access member data.

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello ther
> I read somewhere that callback function should be global or static
> member function.
> 1: I could use static member functions but how could I access members
> of class which ar not static.
> 2: I could use global function but want to access members of class.
> Let me know the possible solutions.
>
> Regards,
> Ajay Sonawane
>



 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      06-06-2005

"kloodge" <(E-Mail Removed)> wrote in message
news:mJZoe.27193$(E-Mail Removed)...

Please don't top-post. [Re-arranged]

> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>> Hello ther
>> I read somewhere that callback function should be global or static
>> member function.
>> 1: I could use static member functions but how could I access members
>> of class which ar not static.
>> 2: I could use global function but want to access members of class.
>> Let me know the possible solutions.
>>
>> Regards,
>> Ajay Sonawane
>>

>


> You may try using a friend function. This would give you a global function
> that can also access member data.
>


While a friend function will allow you access to an object's private or
protected members, it does not help in this case, because a static member or
global function does not have an instance of that class to operate on. The
question was about accessing non-static members, which means he needs an
instance of the class to operate on. Friendship only affects visibility.

-Howard



 
Reply With Quote
 
Rapscallion
Guest
Posts: n/a
 
      06-06-2005
Howard wrote:
> Most callback functions that I've worked with provide a void* pointer, often
> called userData or something similar, which is there for you to use as you
> see fit. What I do is write a global callback function, and put in that
> void* the "this" pointer of the object which wants to receive the callbacks.
> Then, in the callback, I simply cast the void* pointer to a pointer to my
> class, and use it to call a member function on that object to do the actual
> work of the callback.


This is a typical C solution. In C++ you have more and better (i.e.
typesave) options.

 
Reply With Quote
 
BigBrian
Guest
Posts: n/a
 
      06-06-2005
>This is a typical C solution. In C++ you have more and better (i.e.
>typesave) options.


I could be wrong, but I don't think you understand what Rapscallion
posted. This is not a C solution. He's passing the address of a C++
object into the registeration function of the library which is using a
callback. That way when the callback is called, the libary passes back
a pointer to this object which can then used in the callback.

However, this solution depends on the interface of the registration
code providing a user data parameter ( ie something which gets passed
back when the callback is called ). This isn't always the case.

-Brian

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      06-06-2005

"Rapscallion" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Howard wrote:
>> Most callback functions that I've worked with provide a void* pointer,
>> often
>> called userData or something similar, which is there for you to use as
>> you
>> see fit. What I do is write a global callback function, and put in that
>> void* the "this" pointer of the object which wants to receive the
>> callbacks.
>> Then, in the callback, I simply cast the void* pointer to a pointer to my
>> class, and use it to call a member function on that object to do the
>> actual
>> work of the callback.

>
> This is a typical C solution. In C++ you have more and better (i.e.
> typesave) options.
>


The original question was about the callback function, whether it should be
a static member function or a global function, and he was wondering about
how to access non-static members from that function. I told him how I've
generally implemented them and used them to accomplish the task.

This method calls a C++ member function, so it's hardly typical C. And I
can pass the void* pointer as a base class pointer if needed, so that
run-time polymorphism can be used.

It does require that there be a field useable to specify the object instance
when "registering" for the callbacks. This field is usually specified as a
void* pointer, to allow you to pass any kind of object at all for any
purpose you desire. I pass the instance of my object. Since the entire
callback is simply a one-liner that casts my pointer to my _known_ object
type and calls its member function, I don't really have to worry about type
safety. (Assuming I don't get someone else's callbacks...in which case
either the callback mechanism is broken or I didn't RTFM.)

Although you didn't specify an alternate method, I suppose you could use
templates. I haven't thought it out, but it sounds like the best
"all-purpose" solution to me, if you're sticking to C++ callback clients
only.

But I suspect that most API's I've worked with don't really care if you're
calling their library from C or C++ (or even something else), so they just
go with a generic void* pointer. And, I don't recall any API I've used
implementing their callbacks as templates...so far they've all provided a
void* pointer for my use.

-Howard




 
Reply With Quote
 
Rapscallion
Guest
Posts: n/a
 
      06-06-2005
Howard wrote:
> The original question was about the callback function, whether it should be
> a static member function or a global function, and he was wondering about
> how to access non-static members from that function. I told him how I've
> generally implemented them and used them to accomplish the task.
>
> This method calls a C++ member function, so it's hardly typical C. And I
> can pass the void* pointer as a base class pointer if needed, so that
> run-time polymorphism can be used.


You can easily avoid a void* pointer, in C and in C++. And, void* isn't
a 'base class pointer', nor is it 'generic'.

> Although you didn't specify an alternate method, I suppose you could use
> templates. I haven't thought it out, but it sounds like the best
> "all-purpose" solution to me, if you're sticking to C++ callback clients
> only.


Templates? I have no idea what you mean.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      06-06-2005

"Rapscallion" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Howard wrote:
>> The original question was about the callback function, whether it should
>> be
>> a static member function or a global function, and he was wondering about
>> how to access non-static members from that function. I told him how I've
>> generally implemented them and used them to accomplish the task.
>>
>> This method calls a C++ member function, so it's hardly typical C. And I
>> can pass the void* pointer as a base class pointer if needed, so that
>> run-time polymorphism can be used.

>
> You can easily avoid a void* pointer, in C and in C++. And, void* isn't
> a 'base class pointer', nor is it 'generic'.
>


I never suggested that you couldn't avoid a void* pointer. But if the
callback mechanism being used DOES use a void* pointer, then that's what it
uses. (But recall that the OP was talking about the callback function
itself, and never stated what the callback mechanism was, nor what the
parameters passed to that function might be, so speculating is somewhat
useless at this point.)

And I never said void* was a base class pointer. Where did you get that
idea? I said I could pass a base-class pointer AS a void* pointer, in the
specific case I'm describing where a void* pointer is given to the user's
callback function (and previously set by object during the registration
process for the callback).

Also, what isn't generic about a void* pointer? You can pass ANY pointer as
a void* pointer. Sounds pretty generic to me.

>> Although you didn't specify an alternate method, I suppose you could use
>> templates. I haven't thought it out, but it sounds like the best
>> "all-purpose" solution to me, if you're sticking to C++ callback clients
>> only.

>
> Templates? I have no idea what you mean.
>


What do you not understand? Are you asking for clarification of something?
Or, do you not know what templates are? Or, do you disagree that templates
might provide a solution? Or,...?

I guess I just fail to see if you're arguing something here, or asking for
more information, or what. If you've got a specific point to make, make it.
So far, you've made no specific recommendations. You're not providing any
information that the OP can use, nor presenting any discussion that can be
intelligently engaged in.

-Howard



 
Reply With Quote
 
ajay.sonawane@gmail.com
Guest
Posts: n/a
 
      06-07-2005
Thanks Howard,
Passing base class pointer as an extra parameter to callback function
solved my problem. Its pretty easy. Sounds that it is "all time easiest
solution".
But one queston, Is "thunk" related to callback function ? If not
what is it ? Someone just told me that thunk could be used to access
members of class when you use callback functions.
Awaiting for your comments.

 
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
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
Callback functions Pratik C++ 5 06-15-2004 07:27 PM



Advertisments