home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!elroy.jpl.nasa.gov!usc!cs.utexas.edu!devnull!thunder.Berkeley.EDU!rjf
- From: rjf@thunder.Berkeley.EDU (Russell Fleming)
- Newsgroups: comp.lang.c++
- Subject: Re: const * const return value
- Keywords: const pointer
- Message-ID: <2402@devnull.mpd.tandem.com>
- Date: 9 Sep 92 13:41:20 GMT
- References: <1992Sep7.210827.10793@sci.kun.nl>
- Sender: news@devnull.mpd.tandem.com
- Lines: 52
-
- In article <1992Sep7.210827.10793@sci.kun.nl>, leo@sci.kun.nl (Leo
- Willems) writes:
- >
- > Is the returntype const char * const legal when it is assigned to a
- const char*:
- >
- > const char * const
- > f()
- > {
- > return "aa";
- > }
- >
- > void
- > x()
- > {
- > const char * p = f(); // legal assignment??
- >
- > ++p; // sigh
- > }
- >
- > cfront 3.0.1 is accepting this, according to me it breaks the meaning
- > of a const pointer to const. Is there an explanation?
- >
-
- Cfront 3.0.1 is correct in accepting your code without complaint. Your
- example can be re-written as:
- const char * const a = "aa";
- const char *p = a;
-
- The description for 'a' is a const pointer to a const memory location.
- The description for 'p' is a pointer to a const memory location. The
- principal difference here is that the variable 'a' may not be modified
- while the variable 'p' may be modified. NOTE, I said the variable and
- not the memory location pointed to by the variable. The only way the
- meaning of a const pointer to const could be broken is if the compiler
- allowed the variable 'a' to be modified.
-
- If the compiler could not accept your code fragment, then the following
- code fragment could also not be accepted by the compiler since it is, in
- essence, the same thing:
-
- const int a = 5;
- int p = a;
-
- Once again, the const declaration for the variable simply means that the
- variable itself may not be modified, but it is perfectly acceptable to
- transfer the variable's contents to another variable which allows modification.
-
- =================================================================
- Rusty Fleming, Software Consultant @ Tandem Computers Inc.
- email: rjf@mpd.tandem.com Austin, Texas
- voice: (512) 244 - 8390 USA
-