home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / std / cplus / 2025 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.6 KB  |  84 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!think.com!rpi!newsserver.pixel.kodak.com!kodak!acadia!cok
  3. From: cok@acadia.Kodak.COM (David Cok)
  4. Subject: Re: Proposal for default scope
  5. Message-ID: <1993Jan11.153602.22907@kodak.kodak.com>
  6. Sender: news@kodak.kodak.com
  7. Organization: Eastman Kodak Co., Rochester, NY
  8. References: <mkohtala.726108774@vipunen.hut.fi> <1993Jan8.044714.2027@qualcomm.com>
  9. Date: Mon, 11 Jan 93 15:36:02 GMT
  10. Lines: 72
  11.  
  12. In article <1993Jan8.044714.2027@qualcomm.com> greg@qualcom.qualcomm.com (Greg Noel) writes:
  13. >
  14. >That is,
  15. >    class Class {
  16. >    public:  int func1();
  17. >    };
  18. >    Class:: {
  19. >        int helper() { /* ... */ }
  20. >        int func1() { /* code including calls to helper() */ }
  21. >    }
  22. >Since helper() is not declared in the class, it is not exported from the
  23. >scope, while func1() is declared in the class, so it is exported in the
  24. >normal fashion.
  25. >
  26.  
  27. I agree fully with your desire to provide helper functions with access to
  28. a class which do not appear in the class declaration and thus do not
  29. cause recompilation when they are introduced or removed.  This is how I
  30. do it, sticking to existing C++.  It's not as good as having language 
  31. facilities to add helper functions into a class without affecting the 
  32. header file, but for the moment it is adequate.  The drawbacks are 
  33. introduction of another class name (which is publically visible), the 
  34. need for qualification of the helper functions, and being careful about 
  35. derived classes of Class when used in Class_helper.
  36.  
  37. Class.h:
  38.  
  39. class Class {
  40.     friend class Class_helper;
  41.     private: void func_private();
  42.     public: int func1();
  43. };
  44.  
  45.  
  46. Class.C:
  47.  
  48. #include "Class.h"
  49.  
  50. class Class_helper {
  51.     friend class Class;
  52. private:
  53.     static int helper() { ... }
  54.     static int helper2(Class* c) { c->func_private(); ... }
  55. };
  56.  
  57. typedef Class_helper Ch; // abbreviation 
  58. static Class_helper CH; // just for convenience -- see usage examples below
  59.  
  60. void Class::func_private() { ... }
  61.  
  62. int Class::func1() {
  63.     Class_helper::helper(); // take your pick of these three alternatives
  64.     Ch::helper();
  65.     CH.helper();
  66.  
  67.     Ch::helper2(this);
  68. }
  69.  
  70.  
  71. It would be nice to be able to make Class_helper a nested class within Class
  72. while still declaring it in Class.C; but if that were made possible, I imagine 
  73. that the problem which gave rise to this programming idiom could be fixed
  74. directly.
  75.  
  76. One can also derive Class_helper from Class to get at Class's member functions
  77. directly.  Then helper2 does not need to be static and does not need an
  78. argument, but there would need to be a conversion from Class* to Class_helper, 
  79. so there is not a lot of gain.
  80.  
  81. David R. Cok
  82. Eastman Kodak Company
  83. cok@Kodak.COM
  84.