home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11662 < prev    next >
Encoding:
Text File  |  1992-07-28  |  1.8 KB  |  51 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: const on pass by value
  5. Message-ID: <1992Jul27.185931.5433@microsoft.com>
  6. Date: 27 Jul 92 18:59:31 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Jul24.151010.11969@PacBell.COM>
  9. Lines: 40
  10.  
  11. In article <1992Jul24.151010.11969@PacBell.COM> pjcondi@lepton (Paul Condie) writes:
  12. |Should pass by value arguments be declared as "const" for
  13. |good coding style?  Or is that nit picking?
  14. |
  15. |For example:
  16. |
  17. |void foo (const int a);
  18. |or
  19. |void foo (int a);
  20.  
  21. The confusion here is that declaring a param really declares two different
  22. things:
  23.  
  24. 1) It declares the external interface to the function
  25.  
  26. 2) It declares the internal type of the param.
  27.  
  28. In both cases above the external interface is to a function foo taking an
  29. int returning void.
  30.  
  31. The first case makes the further claim that the parameter will be used
  32. as a constant within the function.  This claim about the internal use
  33. of "a" might be very useful to the implementor of the function, the
  34. maintainer of the function, and in some cases might even aid the
  35. code generator in creating better machine code.
  36.  
  37. So, yes, declaring the param "const" is a "good idea" even if the constness
  38. only applies to the "internal" use of the param within the function.
  39.  
  40. Note that this param declaration is a very different situation from 
  41. the similar looking:
  42.  
  43. void foo(const Bar& b)
  44.  
  45. where the "const" represents BOTH an external interface and a internal 
  46. constraint on the use of b [note that people from the Humpty-Dumpty camp would 
  47. claim otherwise that the "const" only refers to the *external* interface, and
  48. that *internal* to foo the function implementor should be free to molest
  49. b as they so desire.  Currently C++ permits such molestation, but I would
  50. claim that people doing so are showing very bad programming style]
  51.