[Bug c++/52185] Const member function may change the object for which the function is called.
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Feb 9 17:12:00 GMT 2012
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52185
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-02-09 17:11:40 UTC ---
[class.this] says, "In a const member function, the object for which the
function is called is accessed through a const access path;"
That doesn't mean the object is immutable.
In the member function you can't change A::a (but you couldn't do that anyway
because it's a reference) but you can change the thing it is bound to, because
it's a non-const reference, and it could point to a non-member just as easily
as to a member e.g.
int i = 0;
struct A {
int a1;
int a2;
A(bool b) : a1(1), a2(b ? a1 : i) { }
void f() const { ++a2; }
};
Should this fail to compile?
What if you only ever call the constructor with a 'false' argument?
More information about the Gcc-bugs
mailing list