holub@violet.berkeley.edu (09/05/90)
Can anyone give me a realistic example of what pointers to class members are good for? I understand how they work and what they do, but all of the books I've seen give examples that are too trivial to be useful, and every time I try to use them I come up against some structural problem that eventually makes me take a different approach.
grunwald@foobar.colorado.edu (Dirk Grunwald) (09/07/90)
>>>>> On 4 Sep 90 22:59:20 GMT, holub@violet.berkeley.edu said:
h> Can anyone give me a realistic example of what pointers to class members
h> are good for? I understand how they work and what they do, but all of the
h> books I've seen give examples that are too trivial to be useful, and
h> every time I try to use them I come up against some structural problem that
h> eventually makes me take a different approach.
--
Take a tasking package. Assume you have two context switching
mechanisms, one where you've saved state because you were preempted
and one where you weren't. The register restore code is different,
because you save different state in each case.
Rather than set a bit indicating the save method used, you simply fill
in a slot with a pointer to one of two member functions (restore for
preemption or simple restore). This eliminates extra tests & branches.
btw, this is how I was planning on handeling multiple restore policies
(perhaps subclassed as well) in my tasking library, so it's not
completely made up.
Dirk Grunwald -- Univ. of Colorado at Boulder (grunwald@foobar.colorado.edu)
(grunwald@boulder.colorado.edu)
gwu@nujoizey.tcs.com (George Wu) (09/11/90)
In article <1990Sep4.225920.14917@agate.berkeley.edu>, holub@violet.berkeley.edu writes: |> Can anyone give me a realistic example of what pointers to class members |> are good for? I understand how they work and what they do, but all of the |> books I've seen give examples that are too trivial to be useful, and |> every time I try to use them I come up against some structural problem that |> eventually makes me take a different approach. We use such pointers for generic menu item and button classes. For instance, we have a generic button which, when pressed, will call the previously defined function: class Caller { // . . . }; class CbButton { CbButton(const char *label, Caller *object, (Caller::*funcPtr)()); Press() }; where Press() looks something like: CbButton::Press() { (object->*funcPtr)(); } We actually use a generic type for CbButton in order to get the type checking correct. And we use such code in all sorts of places. Sure, it's simple, but it's effective. George ---- George J Wu | gwu@tcs.com or ucbcad!tcs!gwu Software Engineer | 2121 Allston Way, Berkeley, CA, 94704 Teknekron Communications Systems, Inc.| (415) 649-3752
garry@alice.UUCP (garry hodgson) (09/14/90)
In article <1990Sep4.225920.14917@agate.berkeley.edu>, holub@violet.berkeley.edu writes: > Can anyone give me a realistic example of what pointers to class members > are good for? I understand how they work and what they do, but all of the > books I've seen give examples that are too trivial to be useful, and > every time I try to use them I come up against some structural problem that > eventually makes me take a different approach. Here are 2 small but useful examples that I use. I use a Generic list class, called a Bunch, which has an Invoke() member function which invokes the specified member function on each of its elements. Thus, for example, in the Bunchimplement macro in my Bunch.h, I might have void Bunch(type)::Print() { Invoke( &type::Print ); } You could use this same approach to build mapping functions like those in Lisp. In InterViews applications, I have a generic Popup menu class. Associated with each menu entry is a callback procedure, which is a member function of the class which owns the menu, and thus has access to whatever it needs to implement the function. garry hodgson