Re: Const'ness


Subject: Re: Const'ness
From: Mike Nordell (tamlin@algonet.se)
Date: Sun Jul 09 2000 - 18:18:47 CDT


sam th wrote:

> On Sat, 8 Jul 2000, Martin Sevior wrote:
>
> > Ah this makes sense. I can't modify the contents of a const pointer. I
> > never do anyway since that goes againt spirit protected member
> > variables.
> > If something needs changing I always use a setForVariable function.
>
> The other good thing about const pointers is they prevent you from trying
> to modify temporaries. If you have the following function
>
> const int foo();
>
> then the compiler won't let you do this:
>
> foo()=6;
>
> whereas if the function did not return const, that statement would be
> legal (and a truly horrid idea).

You're not sure of this sam, are you? :-)
To make really, really sure, try these things out on www.comeaucomputing.com
using the test-drive for Comeau C++ online (this is not a product
endorsement, just one of the most conforming compilers around).

Even without testing, AFAIK:
The return of a temporary (which is what a return-value is) is (by the C++
standard) not a modifiable l-value, and haven't been since at least
Nov 1997 (the predefined macro '__cplusplus' using a conforming compiler is
199711L).

What would make your statement true was if the function looked like:
  const int& foo();
vs.
  int& foo();

In fact, the declaration 'const int foo();' is meaningless, and might well
emit a diagnostic message.

/Mike



This archive was generated by hypermail 2b25 : Sun Jul 09 2000 - 17:18:14 CDT