Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const vs. static const inside class

Reply
Thread Tools

const vs. static const inside class

 
 
John Goche
Guest
Posts: n/a
 
      12-07-2006

Hello,

I would like to know whethere there is a difference between
a const variable and a static const variable inside a class.
After all, if a variable is const in a class, the compiler can
always optimize and turn it into a static const variable to
save runtim memory/stack space since being const it
cannot be changed. (?)

Thanks,

JG

 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      12-07-2006
John Goche wrote:
> Hello,
>
> I would like to know whethere there is a difference between
> a const variable and a static const variable inside a class.
> After all, if a variable is const in a class, the compiler can
> always optimize and turn it into a static const variable to
> save runtim memory/stack space since being const it
> cannot be changed. (?)
>
> Thanks,
>
> JG
>


A non-static constant member variable may have a different constant
value for different instances of the class.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-07-2006
John Goche wrote:
> I would like to know whethere there is a difference between
> a const variable and a static const variable inside a class.
> After all, if a variable is const in a class, the compiler can
> always optimize and turn it into a static const variable to
> save runtim memory/stack space since being const it
> cannot be changed. (?)


I am not sure under what circumstances the non-static const data
member can be optimized away. If the object is non-const to begin
with, something can always cast away the const-ness and change the
data member without much of a concern, really. So, the data member
has to exist per instance. A static const data member exists per
class, so it definitely can be placed in a const storage and using
a const cast to remove const-ness on it would result in undefined
behaviour. That's why it can actually be optimized by the compiler
and be used as a literal in many cases. That's the difference,
AFA I'm concerned.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      12-07-2006

Victor Bazarov wrote:
> John Goche wrote:
> > I would like to know whethere there is a difference between
> > a const variable and a static const variable inside a class.
> > After all, if a variable is const in a class, the compiler can
> > always optimize and turn it into a static const variable to
> > save runtim memory/stack space since being const it
> > cannot be changed. (?)

>
> I am not sure under what circumstances the non-static const data
> member can be optimized away. If the object is non-const to begin
> with, something can always cast away the const-ness and change the
> data member without much of a concern, really.


If the data member is const then casting the object itself to/from
const has no affect on the undefined nature of changing that member.
So I don't think it needs to be a different object per instance for
that reason.

However, since you can initialize const data members with data passed
into the constructor those values can be different per instance to
begin with and can be based on runtime calculated values. For that
reason most of the time the data member needs to be one per instance
and can't be optimized away. I doubt any compiler author would spend
much time looking for the few times when it could be.

A code sample to illustrate for the OP:

struct X
{
const int x;
X() : x(0) {}
X(int y) : x(y) {}
};

int main(void)
{
X x1;
X x2(5);
X x3(9);

std::cout << x1.x << "\n";
std::cout << x2.x << "\n";
std::cout << x3.x << "\n";

int y;
std::cin >> y;

X x4(y);

std::cout << x4.x << "\n";
}

 
Reply With Quote
 
Evan
Guest
Posts: n/a
 
      12-08-2006

Victor Bazarov wrote:
> John Goche wrote:
> > I would like to know whethere there is a difference between
> > a const variable and a static const variable inside a class.
> > After all, if a variable is const in a class, the compiler can
> > always optimize and turn it into a static const variable to
> > save runtim memory/stack space since being const it
> > cannot be changed. (?)

>
> I am not sure under what circumstances the non-static const data
> member can be optimized away. If the object is non-const to begin
> with, something can always cast away the const-ness and change the
> data member without much of a concern, really.


My impression was that if you did this to an object that was declared
const the result was undefined behavior. Am I mistaken on this point?

My understanding was if you have a non-const object that gets turned
into a const object at some point, you can cast away the constness, but
if the object was declared const then you can't be guaranteed that it
will work as you wish.

(Though in the context of this discussion, as other posters have said
there is a difference between const and static const because a const
member just has to be const within any given object, but different
objects may have different values for that variable.)

Evan

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-08-2006
Evan wrote:
> Victor Bazarov wrote:
>> John Goche wrote:
>>> I would like to know whethere there is a difference between
>>> a const variable and a static const variable inside a class.
>>> After all, if a variable is const in a class, the compiler can
>>> always optimize and turn it into a static const variable to
>>> save runtim memory/stack space since being const it
>>> cannot be changed. (?)

>>
>> I am not sure under what circumstances the non-static const data
>> member can be optimized away. If the object is non-const to begin
>> with, something can always cast away the const-ness and change the
>> data member without much of a concern, really.

>
> My impression was that if you did this to an object that was declared
> const the result was undefined behavior. Am I mistaken on this point?


No, but if you do it in a member function that is not declared 'const',
what could be wrong? Don't do it in a c-tor or d-tor, of course.

> My understanding was if you have a non-const object that gets turned
> into a const object at some point, you can cast away the constness,
> but if the object was declared const then you can't be guaranteed
> that it will work as you wish.


Any data member of a non-const object is non-const to begin with (the
memory is allocated in a mutable area of the program full storage).
The 'const' qualifier for a member is a precaution, not a lock.

> (Though in the context of this discussion, as other posters have said
> there is a difference between const and static const because a const
> member just has to be const within any given object, but different
> objects may have different values for that variable.)


Yes. You initialise any non-static data member in the constructor
initialiser list. Since it is possible to give different values to
const non-static members, that's probably why they are non-static.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static const T vs static T const er C++ 3 04-22-2008 10:22 PM
Why static const string = "NA"; is not allowed inside the class. Thinking_In_CPP C++ 3 02-13-2008 08:40 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM



Advertisments