This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/52185] Const member function may change the object for which the function is called.
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 09 Feb 2012 17:11:40 +0000
- Subject: [Bug c++/52185] Const member function may change the object for which the function is called.
- Auto-submitted: auto-generated
- References: <bug-52185-4@http.gcc.gnu.org/bugzilla/>
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?