Ben, that was not what I was looking for. From the perspective of the
user of a class, when someone sees a declaration like
void display() const;
in the class header, he can be assured that the function won't change
the object in any way. Similarly, is there a way to assure the class
user that a particular static member function will not change the
static members of a class in any way?
static void print() const; // this is not allowed by the compiler
Thanks,
Srini
Well, if the user can't see the static member, they won't care if the member
function will change it or not. Or if the static member itself is declared
constant, no one can change its state anyways.
It would be better if you can post your real problem and let the group come
up with better solutions.
The reason why there is a facility to protect member variable from member
function, is that non-const member variables can be constants if the whole
class object is defined as const:
const circle c;
c.draw(); // must be circle::draw() const;
circle c2;
c2.draw(); // const is now meaningless
Note that the member function circle::draw() has to maintain the object,
whether const or not, in a valid state even when it can change the value
anyways! There's no way you can possibly make a static member a const in any
given situation; and i would argue there is really no way you really need a
non-const, non-private static member be protected by any means. This is only
one part of the answer.
Put aside the morality, let's look at the legality of what you have
requested. The way you want it to be can be proved too hard for the type
system to check, consider:
void helper(void);
class Foo
{
public:
static int i;
void f() wont_change_static_member // let's say we have this new
keyword
{
helper();
//...
}
};
void helper(void)
{
Foo::i ++; // easily breaking the new keyword.
}
The type system has no way to defend the constness of a static, public,
non-const declared object, just as it has no way to defend a non-const
global object.
ben