Weird const-ness troubles
From: James Aguilar (jfa1_at_cec.wustl.edu)
Date: 03/03/05
- Next message: john townsley: "multiple files"
- Previous message: David: "Re: client -server interaction over XML supporting multiple protocols"
- Next in thread: Lionel B: "Re: Weird const-ness troubles"
- Reply: Lionel B: "Re: Weird const-ness troubles"
- Reply: Matthias Kaeppler: "Re: Weird const-ness troubles"
- Reply: Rolf Magnus: "Re: Weird const-ness troubles"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 3 Mar 2005 02:26:00 -0600
Take the following code example:
class Array {
double *m_array;
public:
Array() { m_array = new double[10]; }
double *begin() const {return m_array;}
};
int main() {
const Array a = Array();
double *iShouldNotExist(a.begin());
iShouldNotExist[0] = -1000;
return 0;
}
This code compiles and runs on g++ 3.3.3 with -pedantic and -ansi set. As I
see it, this code allows me to take an object declared as const and mess
around with its private internals because of a method call that promises
that the object will not be changed. What's up with that? I know that the
right way is to return a const double *, but shouldn't this kind of thing be
disallowed?
- JFA1
- Next message: john townsley: "multiple files"
- Previous message: David: "Re: client -server interaction over XML supporting multiple protocols"
- Next in thread: Lionel B: "Re: Weird const-ness troubles"
- Reply: Lionel B: "Re: Weird const-ness troubles"
- Reply: Matthias Kaeppler: "Re: Weird const-ness troubles"
- Reply: Rolf Magnus: "Re: Weird const-ness troubles"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|