home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!wupost!csus.edu!netcom.com!nagle
- From: nagle@netcom.com (John Nagle)
- Subject: Re: Not supposed to use static variables in member fns?
- Message-ID: <1993Jan1.042307.26317@netcom.com>
- Organization: Netcom - Online Communication Services (408 241-9760 guest)
- References: <ghawkins.725838896@unix1.tcd.ie>
- Date: Fri, 1 Jan 1993 04:23:07 GMT
- Lines: 48
-
- 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 (sic) visible to all member
- >functions. Is this right? I was doing:
- >class Foo {
- > void bar;
- >}
- >void Foo::bar()
- >{
- > static int remember;
- >}
- >But was rather upset to find that all instances of Foo shared the
- >same 'remember'.
- That's what C++ is supposed to do.
-
- You've been taking this "object" stuff too seriously. There isn't
- a separate copy of each "method" for each object instance. Even
- though class function declarations appear intermixed with the field
- declarations inside a class declaration, they aren't really fields.
- Class instances are really just structures, with the fields declared in
- the class declaration and those of its parents, plus a type field used
- to properly dispatch calls to virtual functions.
-
- >I find this quite surprising, does C++ save you from using
- >global variables at one level by allowing you to pack things up
- >into objects only to force you into using variables that are visible
- >to all the members within an object.
-
- That's about right. You can't hide things as much as some people
- would like. The way C++ uses include files for class declarations limits
- the amount of hiding possible. Note that private fields have to appear
- in the include file used by users of the class, even though the users
- can't access them. This is an even worse visibility problem, and forces
- unnecessary recompilations of class users when the implementation of a class
- changes.
-
- John Nagle
-
- >Yours,
-
-
- >George.
-
- >--
- >george lives at:
- >___________________________________________________________________________
- >| ghawkins@unix1.tcd.ie (mostly) | ghawkins@vax1.tcd.ie (sometimes) |
- >---------------------------------------------------------------------------
-