home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / cplus / 18896 < prev    next >
Encoding:
Text File  |  1993-01-10  |  1.8 KB  |  51 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!rational.com!thor!rmartin
  3. From: rmartin@thor.Rational.COM (Bob Martin)
  4. Subject: Re: Not supposed to use static variables in member fns?
  5. Message-ID: <rmartin.726676735@thor>
  6. Sender: news@rational.com
  7. Organization: Rational
  8. References: <ghawkins.725838896@unix1.tcd.ie>
  9. Date: Sun, 10 Jan 1993 14:38:55 GMT
  10. Lines: 39
  11.  
  12. ghawkins@unix1.tcd.ie (George C. Hawkins) writes:
  13.  
  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?
  18.  
  19. Yes.  Such a variable is called a "member variable".
  20.  
  21. There are two kinds.  Regular member variables are known to all member
  22. functions, but are unique to each instance.  Each instance will have
  23. its own space reserved for the variable.  We use these when we want to
  24. remembers something specific about a single object.  Static member
  25. variables are also known to all member functions, but there is only
  26. one variable regardless of the number of objects.  We use static
  27. member variables when we want to remembers something specific about
  28. the class of objects.
  29.  
  30. class Person
  31. {
  32.   private:
  33.     int itsWeight;
  34.     static int theirAverageWeight;
  35. };
  36.  
  37. In the example above, 'itsWeight' is a regular instance variable.  It
  38. remembers the weight of a Person.  If many Person instances are
  39. created, then each will have its own 'itsWeight' variable recording
  40. the weight for that particular instance.  'theirAverageWeight' is a
  41. static member variable.  Their is only one copy of this variable.  It
  42. remembers the average weight of all the Person instances that exist.
  43.  
  44.  
  45.  
  46. --
  47. Robert Martin                        Training courses offered in:
  48. R. C. M. Consulting                       Object Oriented Analysis
  49. 2080 Cranbrook Rd.                        Object Oriented Design
  50. Green Oaks, Il 60048 (708) 918-1004       C++
  51.