home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Subject: Re: Not supposed to use static variables in member fns?
- Message-ID: <1993Jan1.153855.26566@ucc.su.OZ.AU>
- Sender: news@ucc.su.OZ.AU
- Nntp-Posting-Host: extro.ucc.su.oz.au
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- References: <ghawkins.725838896@unix1.tcd.ie>
- Date: Fri, 1 Jan 1993 15:38:55 GMT
- Lines: 43
-
- In article <ghawkins.725838896@unix1.tcd.ie> ghawkins@unix1.tcd.ie (George C. Hawkins) writes:
- >
- >If a member function wants to remember a value between calls to it
- >then it must store that value in a varaible visible to all member
- >functions. Is this right? I was doing:
- >
- >true. I find this quite surprising, does C++ save you from using
- >global variables at one level by allowing you to pack things up
- >into objects only to force you into using variables that are visible
- >to all the members within an object.
- >
- Yes, more or less: the problem of global data
- is not solved, just reduced by division.
-
- You can solve your problem with several techniques,
- one being to make the function a member of yet another class,
- and put a pointer to it in the original object:
-
- // Originally
-
- class X { int dontcare; int private_to_f;
- f(){ ... private_to_f .. }
- g(){ .. private_to_f .. } // WOOPS
- };
-
- // fixed
-
- class X { int dontcare; Y* y;
- g(){ cant access 'private_to_f'}
- f(){y->f();}
- };
-
- class Y {int private_to_f;
- public: f(){ .. private_to_f .. } };
-
- // Ugly, requires pointer :-(
- // easier to 'trust' the other members
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
-