home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13427 < prev    next >
Encoding:
Internet Message Format  |  1992-09-09  |  2.1 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!usc!cs.utexas.edu!devnull!thunder.Berkeley.EDU!rjf
  2. From: rjf@thunder.Berkeley.EDU (Russell Fleming)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: const * const return value
  5. Keywords: const pointer
  6. Message-ID: <2402@devnull.mpd.tandem.com>
  7. Date: 9 Sep 92 13:41:20 GMT
  8. References: <1992Sep7.210827.10793@sci.kun.nl>
  9. Sender: news@devnull.mpd.tandem.com
  10. Lines: 52
  11.  
  12. In article <1992Sep7.210827.10793@sci.kun.nl>, leo@sci.kun.nl (Leo
  13. Willems) writes:
  14. > Is the returntype const char * const legal when it is assigned to a
  15. const char*:
  16. > const char * const
  17. > f()
  18. > {
  19. >     return "aa";
  20. > }
  21. > void
  22. > x()
  23. > {
  24. >     const char * p = f();   // legal assignment??
  25. >     ++p;            // sigh
  26. > }
  27. > cfront 3.0.1 is accepting this, according to me it breaks the meaning
  28. > of a const pointer to const. Is there an explanation?
  29.  
  30. Cfront 3.0.1 is correct in accepting your code without complaint.  Your
  31. example can be re-written as:
  32.     const char * const a = "aa";
  33.     const char *p = a;
  34.  
  35. The description for 'a' is a const pointer to a const memory location. 
  36. The description for 'p' is a pointer to a const memory location.  The
  37. principal difference here is that the variable 'a' may not be modified
  38. while the variable 'p' may be modified.  NOTE, I said the variable and
  39. not the memory location pointed to by the variable.  The only way the
  40. meaning of a const pointer to const could be broken is if the compiler
  41. allowed the variable 'a' to be modified.
  42.  
  43. If the compiler could not accept your code fragment, then the following
  44. code fragment could also not be accepted by the compiler since it is, in
  45. essence, the same thing:
  46.  
  47.     const int a = 5;
  48.     int p = a;
  49.  
  50. Once again, the const declaration for the variable simply means that the
  51. variable itself may not be modified, but it is perfectly acceptable to
  52. transfer the variable's contents to another variable which allows modification.
  53.  
  54. =================================================================
  55. Rusty Fleming, Software Consultant @ Tandem Computers Inc.
  56. email: rjf@mpd.tandem.com            Austin, Texas
  57. voice: (512) 244 - 8390              USA
  58.