home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!rational.com!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Subject: Re: Not supposed to use static variables in member fns?
- Message-ID: <rmartin.726676735@thor>
- Sender: news@rational.com
- Organization: Rational
- References: <ghawkins.725838896@unix1.tcd.ie>
- Date: Sun, 10 Jan 1993 14:38:55 GMT
- Lines: 39
-
- 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?
-
- Yes. Such a variable is called a "member variable".
-
- There are two kinds. Regular member variables are known to all member
- functions, but are unique to each instance. Each instance will have
- its own space reserved for the variable. We use these when we want to
- remembers something specific about a single object. Static member
- variables are also known to all member functions, but there is only
- one variable regardless of the number of objects. We use static
- member variables when we want to remembers something specific about
- the class of objects.
-
- class Person
- {
- private:
- int itsWeight;
- static int theirAverageWeight;
- };
-
- In the example above, 'itsWeight' is a regular instance variable. It
- remembers the weight of a Person. If many Person instances are
- created, then each will have its own 'itsWeight' variable recording
- the weight for that particular instance. 'theirAverageWeight' is a
- static member variable. Their is only one copy of this variable. It
- remembers the average weight of all the Person instances that exist.
-
-
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-