By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,594 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,594 IT Pros & Developers. It's quick & easy.

static member constness

P: n/a
Hello all,

I was just wondering about this. A const member function guarantees
constness of the object within the function body. But there's no way
for a member function to guarantee the constness of static members. Is
there a way to do that? Also, is there a way for a static member
function to guarantee constness of static members?

TIA
Regards,
Srini

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ben

"Srini" <sr*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello all,

I was just wondering about this. A const member function guarantees
constness of the object within the function body. But there's no way
for a member function to guarantee the constness of static members. Is
there a way to do that? Also, is there a way for a static member
function to guarantee constness of static members?

TIA
Regards,
Srini


Either make the static member const or private?

ben
Jul 23 '05 #2

P: n/a
Either make the static member const or private?


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

Jul 23 '05 #3

P: n/a
Srini wrote:
Either make the static member const or private?
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?

The answer is 'No' (in case you haven't got that yet).
static void print() const; // this is not allowed by the compiler


What would be the point? Const non-static member functions exist because
some instances of the class can be created 'const' (or can be passed into
functions by a reference or a pointer to const), which would require
a special treatment/protection of data members of those instances. OTOH
static data members of the class do not depend on any class instance, and
their inability to change cam come _only_ from their own declaration, which
was suggested.

'const'ness of data members would allow certain optimizations to be applied
inside the member functions declared 'const'. If static data aren't always
const, the same optimization is probably pointless. That's my speculation,
anyway.

V
Jul 23 '05 #4

P: n/a
Victor Bazarov wrote:
Srini wrote:
Either make the static member const or private?


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?

The answer is 'No' (in case you haven't got that yet).
static void print() const; // this is not allowed by the compiler


What would be the point? Const non-static member functions exist because
some instances of the class can be created 'const' (or can be passed into
functions by a reference or a pointer to const), which would require
a special treatment/protection of data members of those instances. OTOH
static data members of the class do not depend on any class instance, and
their inability to change cam come _only_ from their own declaration,
which was suggested.

'const'ness of data members would allow certain optimizations to be
applied
inside the member functions declared 'const'. If static data aren't
always
const, the same optimization is probably pointless. That's my
speculation, anyway.

V


It might be reasonable to create some kind of special class to use as a
static member with const functions, or whatever. You may even be able to
use something like that as a proxy to the static members of the containing
class using a friend specifier, or inheritance relationship.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #5

P: n/a
* Srini:
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


A simple way is to represent the static data of class Foo as an
instance of a class Foo::StaticData, and provide an instance of that
class:

class Foo
{
private:
class StaticData
{
private:
int myValue;
public:
StaticData( int value ): myValue( value ) {}
int value() const { return myValue; }
void setValue( int value ){ myValue = value; }
};
public:
static StaticData statics;
};

Foo::StaticData Foo::statics( 12345 );

Given this possibility there was/is no need to add a special language
feature to achieve the same.

And the above gives more fine-grained control than such a language
feature would be likely to give (although meta-classes would be nice!).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6

P: n/a
ben
> 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
Jul 23 '05 #7

P: n/a
Srini wrote:
Hello all,

I was just wondering about this. A const member function guarantees
constness of the object within the function body. But there's no way
for a member function to guarantee the constness of static members. Is
there a way to do that? Also, is there a way for a static member
function to guarantee constness of static members?


What about something like:

class Handle
{
static int Data;
int get() const { return Data; }
int set(int x) { Data = x; }
};

class AA
{
Handle h;

fread() const;
//...

fmodify();
fmanipulate();
//...
};

This would help from the point of view of not
inadvertently altering the static member in a constant
method (since as long as get() and set() do their
job correctly, the compiler will take care of the rest).
However I don't think it can make the code any _more_
efficient, via optimization, than it would be without
such a construct (Although if a class that used AA was
in the same cpp file a clever compiler might notice
constancy of the 'Data' member).

Regards,
S.K.Mody
Jul 23 '05 #8

P: n/a
I did not have any practical problem in this area, but was just
thinking about why such a thing does not exist. This discussion has
provided me with a lot of insight that I was not aware of. Thanks to
all :)

Regards,
Srini

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.