www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - compiler allows assign to const variable

↑ ↓ ← "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
↑ ↓ -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
→ 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
"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
↑ ↓ → 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