home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18072 < prev    next >
Encoding:
Text File  |  1992-12-16  |  1.4 KB  |  55 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!haven.umd.edu!wam.umd.edu!krc
  3. From: krc@wam.umd.edu (Kevin R. Coombes)
  4. Subject: Re: Static vars in member functions
  5. Message-ID: <1992Dec16.182606.13570@wam.umd.edu>
  6. Sender: usenet@wam.umd.edu (USENET News system)
  7. Nntp-Posting-Host: rac1.wam.umd.edu
  8. Organization: University of Maryland, College Park
  9. References: <1992Dec13.185540.20212@math.ufl.edu>
  10. Date: Wed, 16 Dec 1992 18:26:06 GMT
  11. Lines: 42
  12.  
  13. In article <1992Dec13.185540.20212@math.ufl.edu> X9999BVJ@oak.circa.ufl.edu writes:
  14. >
  15. >When calling a function of a class with a static variable declared in it,
  16. >what is the standardized behaviour for that variable?
  17. >
  18. >E.g.
  19. >
  20. >class Foo
  21. >{
  22. >   public:
  23. >      void Blah( void );
  24. >};
  25. >
  26. >void Foo::Blah( void )
  27. >{
  28. >static int i;
  29. >
  30. >   i++;
  31. >};
  32. >
  33. >void main( void )
  34. >{
  35. >Foo a, b;
  36. >
  37. >   a.Blah();
  38. >   b.Blah();
  39. >   a.Blah();
  40. >}
  41. >
  42. >When the second "a.Blah()" is executed, will i be 2 or 3?
  43. >
  44. >Brian
  45.  
  46. The question is ambiguous, since the value will be 2 when the function is 
  47. entered, and will be 3 when it exits. (Since this is not comp.std.c++,
  48. I did not look for chapter-and-verse in the ARM to support my claim. I
  49. did enter the code into g++ and looked at the result.) The point is that
  50. member functions belong to the class, and not to the object. Since there
  51. is only one version of the function, there is only one version of its
  52. static int i. 
  53.  
  54. Kevin Coombes <krc@math.umd.edu>
  55.