www.digitalmars.com         C & C++   DMDScript  

c++ - compiler allows assign to const variable

reply "Steve Strand" <snstrand comcast.net> writes:
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
parent reply -scooter- <scottm cs.ucla.edu> writes:
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)

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
next sibling parent Heinz Saathoff <hsaat bre.ipnet.de> writes:
Hello,

-scooter- wrote...
 void foo(test const& aa)

I read that declaration as "a constant reference to test", not "a reference to constant test", which would be "const test &".

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 - Heinz
Apr 02 2004
prev sibling parent reply "Matthew" <matthew stlsoft.org> writes:
"-scooter-" <scottm cs.ucla.edu> wrote in message
news:c4ho5t$pnd$1 digitaldaemon.com...
 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)

I read that declaration as "a constant reference to test", not "a

 to 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
parent Scott Michel <scottm cs.ucla.edu> writes:
Matthew wrote:

 Version 8.40.2 of the compiler does not catch this const violation.


 struct test {
     int data[5];
 };

 void foo(test const& aa)

I read that declaration as "a constant reference to test", not "a

 to 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);

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.
Apr 06 2004