May 9, 2010 at 6:31pm May 9, 2010 at 6:31pm UTC
we know that const function cant change object.can they change a static member of object?
i try that,and it can change it!why?
class c{
static int d;
void f() const {d=0;} //!
}
int c::d;
...
it has no error
May 9, 2010 at 7:19pm May 9, 2010 at 7:19pm UTC
Because , static member means , a static memory field belong the class itself.
In constant function , you can't change the members that belong the object. Static member doesn't belong any object, it belongs class.
Sorry for my bad English.I hope , i could clearly explained it.
May 9, 2010 at 7:30pm May 9, 2010 at 7:30pm UTC
i read in forums that const function cant change member of objects .then why function f() in your code can change static int d and have no error?
May 9, 2010 at 7:32pm May 9, 2010 at 7:32pm UTC
Because of what Dufresne said.
Const functions just mean that 'this' is const. So you can't change any members of 'this'.
'd', however, is static, and is therefore not part of 'this', and therefore is not constant.
Static members are basically globals, just with a limited scope.
May 9, 2010 at 7:36pm May 9, 2010 at 7:36pm UTC
thank to Dufresne and others.
@Disch : sorry ,im new with forums