Callback functions problem

A

ajay.sonawane

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
 
H

Howard

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
 
K

kloodge

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

Howard

Please don't top-post. [Re-arranged]
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
 
R

Rapscallion

Howard said:
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.
 
B

BigBrian

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
 
H

Howard

Rapscallion said:
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
 
R

Rapscallion

Howard said:
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.
 
H

Howard

Rapscallion said:
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.
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
 
A

ajay.sonawane

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

Howard

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
 
R

Richard Herring

In message said:
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.
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top