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

Callback functions problem

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

Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

<aj***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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

Jul 23 '05 #2

P: n/a
You may try using a friend function. This would give you a global function
that can also access member data.

<aj***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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

Jul 23 '05 #3

P: n/a

"kloodge" <mg@aol.com> wrote in message
news:mJ*****************@tornado.texas.rr.com...

Please don't top-post. [Re-arranged]
<aj***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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

Jul 23 '05 #4

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

Jul 23 '05 #5

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

Jul 23 '05 #6

P: n/a

"Rapscallion" <ra********@spambob.com> wrote in message
news:11**********************@g47g2000cwa.googlegr 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


Jul 23 '05 #7

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

Jul 23 '05 #8

P: n/a

"Rapscallion" <ra********@spambob.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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

Jul 23 '05 #9

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

Jul 23 '05 #10

P: n/a

<aj***********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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.


Glad I could help!

The only place I recall hearing "thunk" was referring to running Win16
applications in a Win32 environment, or something like that. Sorry, I don't
know how it might be applied here. You could try searching on
groups.google.com and see what others have said about it before.

-Howard
Jul 23 '05 #11

P: n/a
In message <dP*********************@bgtnsc05-news.ops.worldnet.att.net>,
Howard <al*****@hotmail.com> writes

<aj***********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegr oups.com...
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.


Glad I could help!

The only place I recall hearing "thunk" was referring to running Win16
applications in a Win32 environment, or something like that. Sorry, I don't
know how it might be applied here. You could try searching on
groups.google.com and see what others have said about it before.


IIRC (but it was a long time ago) the term is used in two ways:
(a) compiler-generated code which transforms function arguments when
passing them between incompatible interfaces (e.g. 16/32 bits, as above)

(b) an anonymous compiler-generated function used by Algol 60(?) to
allow an expression to be used as an actual argument passed by reference
(and re-evaluated each time the formal parameter was used), giving the
effect of a lambda function.

--
Richard Herring
Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.