473,401 Members | 2,139 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,401 software developers and data experts.

How you handle this?

Ne
The first question is about coding a callback on Windows. In a class of
callback function, the function has to be static. Now the problem is that
from within this function, no non-static members can be accessed or invoked.
Seems like the only way is to change all the members to static.

I'd like to hear your opinions on such change.

Second question is generally how to implement callback in C++.

Thank you very much!


Jul 22 '05 #1
8 1297
Ne wrote:
The first question is about coding a callback on Windows.
Then post it on a Windows programming group, not here.

http://www.slack.net/~shiva/welcome.txt

Second question is generally how to implement callback in C++.


I suppose it depends on what exactly a "callback" is. It's not defined
by the C++ standard as far as I know. In order to get a good answer you
might need to clarify.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2

"Ne" <ne**@ecm.org> wrote in message
news:6Y********************@bgtnsc05-news.ops.worldnet.att.net...
The first question is about coding a callback on Windows. In a class of
callback function, the function has to be static. Now the problem is that
from within this function, no non-static members can be accessed or invoked. Seems like the only way is to change all the members to static.

Not at all.
I'd like to hear your opinions on such change.


It would kind of defeat the object of having a class if you made all the
functions static. You might as well have a bunch of global functions and
variables.

Here's how you should do it, this only works of the callback accepting
function is well designed. Fortunately in Windows most of them are.

I'll illustrate with the Windows API call EnumWindows, but this technique is
widely applicable.

The EnumWindows function requires a callback of the form

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

This is what you must supply (although the name is up to you obviously), it
cannot be a member function, strictly speaking in cannot even be a static
member function although many compilers accept this. Suppose you want to
callback a method called addWindow in a WindowList class. Here's what you do

class WindowList
{
bool addWindow(HWND win);
};

BOOL CALLBACK EnumWindowsGlue(HWND hwnd, LPARAM lParam)
{
return reinterpret_cast<WindowList*>(lParam)->addWindow(hwnd);
};

WindowList myWindowList;
EnumWindows(EnumWindowsGlue, reinterprest_cast<LPARAM>(&myWindowList));

The way you pass the object you want to call a method on as a parameter to
EnumWindows and the way that parameter is recieved by the callback function
is the trick. The callback function is just a 'glue' that lets you attach
the member function to the EnumWindows routine.

Most Windows APIs have similar features, usually the extra parameter is
defined as LPVOID which is a bit more portable and means you only need one
static_cast instead of two reinterpret_cast's. (For the benefit of
non-Windows users, LPARAM is typedef'd as long, and LPVOID is typedef'd as
void*.

john
Jul 22 '05 #3

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Tc*******************@newsread2.news.pas.eart hlink.net...
Ne wrote:
The first question is about coding a callback on Windows.


Then post it on a Windows programming group, not here.

http://www.slack.net/~shiva/welcome.txt


Well it isn't really, its the usual newbie question about using a member
function in a routine that only accepts a pointer to a free function.

john
Jul 22 '05 #4
"Ne" <ne**@ecm.org> wrote in
news:6Y********************@bgtnsc05-news.ops.worldnet.att.net:
The first question is about coding a callback on Windows. In a class
of callback function, the function has to be static. Now the problem
is that from within this function, no non-static members can be
accessed or invoked. Seems like the only way is to change all the
members to static.
You have to arrange to store the pointer to the object somewhere and
invoke the non-static function from the static function using that saved
pointer. If the callback function has a general-purpose argument, you can
use that to pass the object pointer.

For specific Windows callback functions, ask in a group that deals with
the Windows API. This group deals literally with the C++ langauge and
standard library.
Second question is generally how to implement callback in C++.


In C++ a callback is usually a call to a member function through a
pointer to a base class with one or more virtual functions (usually
pure). You implement a specific callback function in the derived class.

Gregg
Jul 22 '05 #5
> The first question is about coding a callback on Windows. In a class of
callback function, the function has to be static. Now the problem is that
from within this function, no non-static members can be accessed or invoked.
Seems like the only way is to change all the members to static.

I'd like to hear your opinions on such change.

Second question is generally how to implement callback in C++.


I dont know the prototype of your callback
Many times, callbacks has user defined parameters in their parameter list
(or inside a structure parameter)
If your callback has, pass "this" with cast
(many times user defined parameters are void*)
Jul 22 '05 #6
> The first question is about coding a callback on Windows. In a class of
callback function, the function has to be static. Now the problem is that
from within this function, no non-static members can be accessed or invoked.
Seems like the only way is to change all the members to static.

I'd like to hear your opinions on such change.

Second question is generally how to implement callback in C++.


I dont know the prototype of your callback
Many times, callbacks has user defined parameters in their parameter list
(or inside a structure parameter)
If your callback has, pass "this" with cast
(many times user defined parameters are void*)
Jul 22 '05 #7
John Harrison wrote:
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Tc*******************@newsread2.news.pas.eart hlink.net...
Ne wrote:
The first question is about coding a callback on Windows.


Then post it on a Windows programming group, not here.

http://www.slack.net/~shiva/welcome.txt

Well it isn't really, its the usual newbie question about using a member
function in a routine that only accepts a pointer to a free function.


Hm... Well, I took his word for it. Perhaps that was a mistake.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #8
John Harrison wrote:
"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:Tc*******************@newsread2.news.pas.eart hlink.net...
Ne wrote:
The first question is about coding a callback on Windows.


Then post it on a Windows programming group, not here.

http://www.slack.net/~shiva/welcome.txt

Well it isn't really, its the usual newbie question about using a member
function in a routine that only accepts a pointer to a free function.


Hm... Well, I took his word for it. Perhaps that was a mistake.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Tim Clacy | last post by:
Please illuminate; what operator of class 'Event' will get matched for these two cases : Event ev1; Event ev2; // Case 1 // if (ev1) ;
7
by: Tony Johansson | last post by:
Hello!! Assume I have a handle body pattern with classes called Handle and Body. In the Body class I store one int value for example 7 or some other integer value. In the Handle class I have...
0
by: Tony Johansson | last post by:
Hello! Here I have two classes these are called Handle and Body and a main. You have the class definition below. Some basic information. In the Handle class is there a pointer to the Body. Each...
2
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to...
14
by: Howard | last post by:
Hi, I recently had a problem where I decided to store objects in a vector. (Previously, I had always stored pointers in vectors). Well, naturally, when storing an object in a vector, using...
2
by: mlc | last post by:
I have a server (in win32 C++, TCP) that listens and accepts socket calls, then via CreateProcess and handle inheritence over the command line starts up a process to handle the client request. Is...
5
by: RichG | last post by:
I'm looking for a way to bring an open form to the top. I know that is I open a form directly with form1.show() and then later, while the form is open I do another form1.show(), that I will get...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
3
by: Peterwkc | last post by:
Hello all C++ expert programmer, I have a handle class which point to another class and use the pointer as object. I follow the code from C++ articles submited by someone in this forum....
7
by: Shraddha | last post by:
What is exactly a handle to an object????Is it same as reference....
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.