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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!psinntp!cci632!dwr
  3. From: dwr@cci632.cci.com (Donald W. Rouse II)
  4. Subject: Re: Not supposed to use static variables in member fns?
  5. Message-ID: <1993Jan12.174324.21992@cci632.cci.com>
  6. Organization: [Computer Consoles, Inc., Rochester, NY
  7. References: <ghawkins.725838896@unix1.tcd.ie> <1993Jan1.153855.26566@ucc.su.OZ.AU>
  8. Date: Tue, 12 Jan 1993 17:43:24 GMT
  9. Lines: 39
  10.  
  11. In article <1993Jan1.153855.26566@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  12. >In article <ghawkins.725838896@unix1.tcd.ie> ghawkins@unix1.tcd.ie (George C. Hawkins) writes:
  13. >>
  14. >>If a member function wants to remember a value between calls to it
  15. >>then it must store that value in a varaible visible to all member
  16. >>functions. [...]
  17. > [...]
  18. >    You can solve your problem with several techniques,
  19. >one being to make the function a member of yet another class,
  20. >and put a pointer to it in the original object:
  21. >
  22. >// Originally
  23. >
  24. >    class X { int dontcare; int private_to_f; 
  25. >        f(){  ... private_to_f .. }
  26. >        g(){ .. private_to_f .. } // WOOPS
  27. >    };
  28. >
  29. >// fixed
  30. >
  31. >    class X { int dontcare; Y* y;
  32. >        g(){ cant access 'private_to_f'}
  33. >        f(){y->f();}
  34. >    };
  35. >
  36. >    class Y {int private_to_f; 
  37. >        public: f(){ .. private_to_f .. } };
  38. >
  39. >// Ugly, requires pointer :-(
  40.  
  41. Why?  Why can't you have:
  42.  
  43.     class Y {int private_to_f; 
  44.         public: f(){ .. private_to_f .. } };
  45.  
  46.     class X { int dontcare; Y y;
  47.         g(){ cant access 'private_to_f'}
  48.         f(){y.f();}
  49.     };
  50.