const function

May 9, 2010 at 6:31pm
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:08pm
no reply?!
why?
May 9, 2010 at 7:19pm
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:22pm
no reply?!
why?


Because you only waited a half hour.

We aren't sitting on these forums 24/7 constantly refreshing our browser to look for new questions. Give us time to respond.
May 9, 2010 at 7:23pm
There's no way that can compile. You're missing at least one semicolon.

Anyways:
http://msdn.microsoft.com/en-us/library/s1sb61xd(VS.80).aspx

What does this imply?

-Albatross
May 9, 2010 at 7:30pm
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
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
thank to Dufresne and others.

@Disch : sorry ,im new with forums
Topic archived. No new replies allowed.