home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!psinntp!cci632!dwr
- From: dwr@cci632.cci.com (Donald W. Rouse II)
- Subject: Re: Not supposed to use static variables in member fns?
- Message-ID: <1993Jan12.174324.21992@cci632.cci.com>
- Organization: [Computer Consoles, Inc., Rochester, NY
- References: <ghawkins.725838896@unix1.tcd.ie> <1993Jan1.153855.26566@ucc.su.OZ.AU>
- Date: Tue, 12 Jan 1993 17:43:24 GMT
- Lines: 39
-
- In article <1993Jan1.153855.26566@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
- >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. [...]
- > [...]
- > 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 :-(
-
- Why? Why can't you have:
-
- class Y {int private_to_f;
- public: f(){ .. private_to_f .. } };
-
- class X { int dontcare; Y y;
- g(){ cant access 'private_to_f'}
- f(){y.f();}
- };
-