c++ - compiler allows assign to const variable
- Steve Strand (13/13) Mar 31 2004 Version 8.40.2 of the compiler does not catch this const violation.
- -scooter- (7/15) Apr 01 2004 ^^^^^^^^^^ - constant reference, not constant data?
- Heinz Saathoff (12/17) Apr 02 2004 No, aa is a reference to a const test. The above declaration is the same...
- Matthew (12/24) Apr 03 2004 reference
- Scott Michel (5/26) Apr 06 2004 I sit corrected. After looking at Dan Saks' writing on the subject (his ...
Version 8.40.2 of the compiler does not catch this const violation. struct test { int data[5]; }; void foo(test const& aa) { aa.data[0]= 0; //compiler allows assign to const variable } int main() { test aa; foo(aa); }
Mar 31 2004
Steve Strand wrote:Version 8.40.2 of the compiler does not catch this const violation. struct test { int data[5]; }; void foo(test const& aa)^^^^^^^^^^ - constant reference, not constant data? I read that declaration as "a constant reference to test", not "a reference to constant test", which would be "const test &". constant references are redundant, since a reference can't be reseated. Thus, it's not a bug, it's really a feature. :-) -scooter
Apr 01 2004
Hello, -scooter- wrote...No, aa is a reference to a const test. The above declaration is the same as void foo(const test& aa); I don't know if const is allowed for references (I assume not because it makes no sense as you already stated), but if, than a const ref could be declared as void foo(test &const a); Using a pointer instead of reference such a declaration makes sense: void foo(test * const a); // a is a const pointer to test - Heinzvoid foo(test const& aa)^^^^^^^^^^ - constant reference, not constant data? I read that declaration as "a constant reference to test", not "a reference to constant test", which would be "const test &".
Apr 02 2004
"-scooter-" <scottm cs.ucla.edu> wrote in message news:c4ho5t$pnd$1 digitaldaemon.com...Steve Strand wrote:referenceVersion 8.40.2 of the compiler does not catch this const violation. struct test { int data[5]; }; void foo(test const& aa)^^^^^^^^^^ - constant reference, not constant data? I read that declaration as "a constant reference to test", not "ato constant test", which would be "const test &".Then you read it wrong. It is a reference to const data, whether void foo(test const& aa); or void foo(const test& aa); What you're talking about would be a void foo(test& const aa); which is not allowed because it is superfluous. A reference cannot be "re-pointed", so there's no need to account for such a qualifier. That's the whole point of references.
Apr 03 2004
Matthew wrote:I sit corrected. After looking at Dan Saks' writing on the subject (his wife teaches CS at my undergrad institution): While legit, I don't agree with the notation as it leads to incorrect interpretation, like mine. Of course, I've disagreed with Dan before.referenceVersion 8.40.2 of the compiler does not catch this const violation. struct test { int data[5]; }; void foo(test const& aa)^^^^^^^^^^ - constant reference, not constant data? I read that declaration as "a constant reference to test", not "ato constant test", which would be "const test &".Then you read it wrong. It is a reference to const data, whether void foo(test const& aa); or void foo(const test& aa);
Apr 06 2004