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

const vs. static const inside class

P: n/a

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

Dec 7 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Dec 7 '06 #2

P: n/a
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
Dec 7 '06 #3

P: n/a

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";
}

Dec 7 '06 #4

P: n/a

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

Dec 8 '06 #5

P: n/a
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
Dec 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.