static member constness

S

Srini

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
 
B

ben

Srini said:
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
 
S

Srini

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
 
V

Victor Bazarov

Srini said:
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
 
S

Steven T. Hatton

Victor said:
The answer is 'No' (in case you haven't got that yet).


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.
 
A

Alf P. Steinbach

* 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!).
 
B

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
 
S

S.K.Mody

Srini said:
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
 
S

Srini

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,298
Messages
2,571,542
Members
48,284
Latest member
RedaBruno6

Latest Threads

Top