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

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!spool.mu.edu!agate!dog.ee.lbl.gov!network.ucsd.edu!qualcom.qualcomm.com!qualcom.qualcomm.com!greg
  3. From: greg@qualcom.qualcomm.com (Greg Noel)
  4. Subject: Re: Proposal for default scope
  5. Message-ID: <1993Jan8.044714.2027@qualcomm.com>
  6. Sender: news@qualcomm.com
  7. Nntp-Posting-Host: qualcom.qualcomm.com
  8. Organization: Qualcomm, Inc., San Diego, CA
  9. References: <mkohtala.726108774@vipunen.hut.fi>
  10. Date: Fri, 8 Jan 1993 04:47:14 GMT
  11. Lines: 71
  12.  
  13. In article <mkohtala.726108774@vipunen.hut.fi> Marko.Kohtala@hut.fi suggests
  14. the use of Class::{ /* ... */ } as syntactic sugar so that class members
  15. could be defined in the scope without the need to explicitly put the class
  16. prefix on each member.  He argues that this would make the code easier to
  17. write and maintain.
  18.  
  19. His example is that
  20. >
  21. >Class::{
  22. >  int var;
  23. >  int func1() { /* ... */ }
  24. >}
  25. >
  26. >... would be treated as
  27. >
  28. >int Class::var;
  29. >int Class::func1() { /* ... */ }
  30.  
  31. I don't know if I agree with that reason, but I would like to see something
  32. like this as a clean way to allow ``helper functions'' in the implementation
  33. of the class.  This particular syntax has the advantage of being intuitive,
  34. since ``Class::'' already means ``open the class scope in this context'' in
  35. other places.
  36.  
  37. The semantics would be that all names defined in the scope would be visible
  38. within the scope (subject to the usual scoping/visibility rules, of course);
  39. names that were declared in the class description would be the only ones
  40. exported from the scope.  This would allow a helper function to be defined
  41. that had full access to the class private members, but still be type-safe,
  42. since the only way to access the helper functions would be from within the
  43. scope and the only way to enter the scope would be via the class interface.
  44.  
  45. That is,
  46.     class Class {
  47.     public:  int func1();
  48.     };
  49.     Class:: {
  50.         int helper() { /* ... */ }
  51.         int func1() { /* code including calls to helper() */ }
  52.     }
  53. Since helper() is not declared in the class, it is not exported from the
  54. scope, while func1() is declared in the class, so it is exported in the
  55. normal fashion.
  56.  
  57. I like it.  It's a way of sneaking up on modules/packages without adding
  58. any new tokens to the language.
  59.  
  60. It does have the disadvantage that it makes the language larger.  On the
  61. other hand, it would seem to eliminate the desire, expressed by several
  62. people, for nested functions.  Such nested functions would usually be
  63. helper functions in the implementation of a class, so by being able to
  64. expand the class context to include non-nested functions would provide
  65. an efficient solution.
  66.  
  67. So what do other people think?  It may be (probably is?) too late to
  68. include this in the current C++ standardization effort, but if there is
  69. some consensus that this syntax is reasonable, it might be possible to
  70. nudge some vendors to try it as an extension.  (If it isn't too late,
  71. how would I go about proposing it to the standards committee?)
  72.  
  73. If you grant that helper functions are needed, but don't like this idea
  74. because it adds to the size of the language, an alternative would be to
  75. take something that is syntactically legal but semantically forbidden and
  76. define semantics for it.  I have thought about
  77.     static /* type */ Class::Helper( /* params */) { /* ... */ }
  78. which currently has no semantics (class functions cannot be defined as
  79. static) and treat it as if it were declared (at file scope) as a private
  80. member of the class.  It would only be accessible to members and friends
  81. within the same file, so it too should be type-safe.
  82. -- 
  83. -- Greg Noel, Unix Guru         greg@qualcomm.com  or  greg@noel.cts.com
  84.