Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!ub!boulder!grunwald From: grunwald@foobar.colorado.edu (Dirk Grunwald) Newsgroups: comp.lang.c++ Subject: Re: What are pointers-to-members good for? Message-ID: <25811@boulder.Colorado.EDU> Date: 7 Sep 90 04:13:57 GMT References: <1990Sep4.225920.14917@agate.berkeley.edu> Sender: news@boulder.Colorado.EDU Reply-To: grunwald@foobar.colorado.edu Organization: University of Colorado at Boulder Lines: 25 In-reply-to: holub@violet.berkeley.edu's message of 4 Sep 90 22:59:20 GMT >>>>> 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)