home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18623 < prev    next >
Encoding:
Text File  |  1993-01-01  |  1.8 KB  |  56 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: Not supposed to use static variables in member fns?
  5. Message-ID: <1993Jan1.153855.26566@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <ghawkins.725838896@unix1.tcd.ie>
  10. Date: Fri, 1 Jan 1993 15:38:55 GMT
  11. Lines: 43
  12.  
  13. In article <ghawkins.725838896@unix1.tcd.ie> ghawkins@unix1.tcd.ie (George C. Hawkins) writes:
  14. >
  15. >If a member function wants to remember a value between calls to it
  16. >then it must store that value in a varaible visible to all member
  17. >functions. Is this right? I was doing:
  18. >
  19. >true. I find this quite surprising, does C++ save you from using
  20. >global variables at one level by allowing you to pack things up
  21. >into objects only to force you into using variables that are visible
  22. >to all the members within an object.
  23. >
  24.     Yes, more or less: the problem of global data
  25. is not solved, just reduced by division.
  26.  
  27.     You can solve your problem with several techniques,
  28. one being to make the function a member of yet another class,
  29. and put a pointer to it in the original object:
  30.  
  31. // Originally
  32.  
  33.     class X { int dontcare; int private_to_f; 
  34.         f(){  ... private_to_f .. }
  35.         g(){ .. private_to_f .. } // WOOPS
  36.     };
  37.  
  38. // fixed
  39.  
  40.     class X { int dontcare; Y* y;
  41.         g(){ cant access 'private_to_f'}
  42.         f(){y->f();}
  43.     };
  44.  
  45.     class Y {int private_to_f; 
  46.         public: f(){ .. private_to_f .. } };
  47.  
  48. // Ugly, requires pointer :-(
  49. // easier to 'trust' the other members
  50.  
  51. -- 
  52. ;----------------------------------------------------------------------
  53.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  54.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  55. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  56.