home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!haven.umd.edu!wam.umd.edu!krc
- From: krc@wam.umd.edu (Kevin R. Coombes)
- Subject: Re: Static vars in member functions
- Message-ID: <1992Dec16.182606.13570@wam.umd.edu>
- Sender: usenet@wam.umd.edu (USENET News system)
- Nntp-Posting-Host: rac1.wam.umd.edu
- Organization: University of Maryland, College Park
- References: <1992Dec13.185540.20212@math.ufl.edu>
- Date: Wed, 16 Dec 1992 18:26:06 GMT
- Lines: 42
-
- In article <1992Dec13.185540.20212@math.ufl.edu> X9999BVJ@oak.circa.ufl.edu writes:
- >
- >When calling a function of a class with a static variable declared in it,
- >what is the standardized behaviour for that variable?
- >
- >E.g.
- >
- >class Foo
- >{
- > public:
- > void Blah( void );
- >};
- >
- >void Foo::Blah( void )
- >{
- >static int i;
- >
- > i++;
- >};
- >
- >void main( void )
- >{
- >Foo a, b;
- >
- > a.Blah();
- > b.Blah();
- > a.Blah();
- >}
- >
- >When the second "a.Blah()" is executed, will i be 2 or 3?
- >
- >Brian
-
- The question is ambiguous, since the value will be 2 when the function is
- entered, and will be 3 when it exits. (Since this is not comp.std.c++,
- I did not look for chapter-and-verse in the ARM to support my claim. I
- did enter the code into g++ and looked at the result.) The point is that
- member functions belong to the class, and not to the object. Since there
- is only one version of the function, there is only one version of its
- static int i.
-
- Kevin Coombes <krc@math.umd.edu>
-