home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18617 < prev    next >
Encoding:
Text File  |  1992-12-31  |  2.2 KB  |  59 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!wupost!csus.edu!netcom.com!nagle
  3. From: nagle@netcom.com (John Nagle)
  4. Subject: Re: Not supposed to use static variables in member fns?
  5. Message-ID: <1993Jan1.042307.26317@netcom.com>
  6. Organization: Netcom - Online Communication Services  (408 241-9760 guest) 
  7. References: <ghawkins.725838896@unix1.tcd.ie>
  8. Date: Fri, 1 Jan 1993 04:23:07 GMT
  9. Lines: 48
  10.  
  11. ghawkins@unix1.tcd.ie (George C. Hawkins) writes:
  12. >If a member function wants to remember a value between calls to it
  13. >then it must store that value in a varaible (sic) visible to all member
  14. >functions. Is this right? I was doing:
  15. >class Foo {
  16. >    void bar;
  17. >}
  18. >void Foo::bar()
  19. >{
  20. >    static int remember;
  21. >}
  22. >But was rather upset to find that all instances of Foo shared the
  23. >same 'remember'. 
  24.       That's what C++ is supposed to do.
  25.  
  26.       You've been taking this "object" stuff too seriously.  There isn't
  27. a separate copy of each "method" for each object instance.  Even
  28. though class function declarations appear intermixed with the field
  29. declarations inside a class declaration, they aren't really fields.
  30. Class instances are really just structures, with the fields declared in
  31. the class declaration and those of its parents, plus a type field used
  32. to properly dispatch calls to virtual functions.
  33.  
  34. >I find this quite surprising, does C++ save you from using
  35. >global variables at one level by allowing you to pack things up
  36. >into objects only to force you into using variables that are visible
  37. >to all the members within an object.
  38.  
  39.       That's about right.  You can't hide things as much as some people
  40. would like.  The way C++ uses include files for class declarations limits
  41. the amount of hiding possible.   Note that private fields have to appear
  42. in the include file used by users of the class, even though the users
  43. can't access them.  This is an even worse visibility problem, and forces
  44. unnecessary recompilations of class users when the implementation of a class 
  45. changes.
  46.  
  47.                     John Nagle
  48.  
  49. >Yours,
  50.  
  51.  
  52. >George.
  53.  
  54. >--
  55. >george lives at:
  56. >___________________________________________________________________________
  57. >|   ghawkins@unix1.tcd.ie (mostly)  |   ghawkins@vax1.tcd.ie (sometimes)  |
  58. >---------------------------------------------------------------------------
  59.