home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18810 < prev    next >
Encoding:
Internet Message Format  |  1993-01-07  |  1.7 KB

  1. Path: sparky!uunet!spool.mu.edu!enterpoop.mit.edu!eru.mt.luth.se!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Not supposed to use static variables in me
  5. Message-ID: <5503@miramon.lulea.trab.se>
  6. Date: 7 Jan 93 16:40:23 GMT
  7. References: <1993Jan5.143723.7928@spectrum.xerox.com>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 31
  10. X-Newsreader: TIN [version 1.1 + PL8]
  11.  
  12. Jim Heliotis (Heliotis.ROCH803@xerox.com) wrote:
  13. : In article 725838896@unix1.tcd.ie, ghawkins@unix1.tcd.ie (George C. Hawkins) writes:
  14. : >
  15. : >If a member function wants to remember a value between calls to it
  16. : >then it must store that value in a varaible visible to all member
  17. : >functions. Is this right? I was doing:
  18. [...]
  19. : >void Foo::bar()
  20. : >{
  21. : >    static int remember;
  22. [...]
  23. : >But was rather upset to find that all instances of Foo shared the
  24. : >same 'remember'.
  25.  
  26.  
  27. : I have not yet seen the answer to this question that I consider most
  28. : straightforward.  You should declare "remember" as a member variable
  29. : (non-static).  The only disadvantage to that, possibly, is that any
  30. : member function can access it that way.
  31.  
  32. Interestingly, introducing "remember" as a _protected_ (non-static)
  33. data member in a _base_ class gives him the added protection.  A given
  34. member function in the (now derived) class can only access "remember"
  35. through the "this" pointer.  Of course, "remember" is now open to attack
  36. from further subclasses.
  37.  
  38. -- 
  39. --------------------------------------------------------------------------
  40. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  41. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  42. --------------------------------------------------------------------------
  43.