home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: Not supposed to use static variables in me
- Message-ID: <5503@miramon.lulea.trab.se>
- Date: 7 Jan 93 16:40:23 GMT
- References: <1993Jan5.143723.7928@spectrum.xerox.com>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 31
- X-Newsreader: TIN [version 1.1 + PL8]
-
- Jim Heliotis (Heliotis.ROCH803@xerox.com) wrote:
- : In article 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:
- [...]
- : >void Foo::bar()
- : >{
- : > static int remember;
- [...]
- : >But was rather upset to find that all instances of Foo shared the
- : >same 'remember'.
-
-
- : I have not yet seen the answer to this question that I consider most
- : straightforward. You should declare "remember" as a member variable
- : (non-static). The only disadvantage to that, possibly, is that any
- : member function can access it that way.
-
- Interestingly, introducing "remember" as a _protected_ (non-static)
- data member in a _base_ class gives him the added protection. A given
- member function in the (now derived) class can only access "remember"
- through the "this" pointer. Of course, "remember" is now open to attack
- from further subclasses.
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-