Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "static const int" versus "const int"

Reply
Thread Tools

"static const int" versus "const int"

 
 
John Goche
Guest
Posts: n/a
 
      11-11-2011

Hello,

I would like to ask what the difference is between
"static const int" versus "const int". I mean of course
the former goes in the static data segment of code
whereas the latter is pushed on the stack, but since
a const does not change it might as well be declared
static, but to what advantage.

What I am asking is how to decide whether to use
"static const int" versus "const int".

Thanks,

John Goche
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-11-2011
On 11/11/2011 11:39 AM, John Goche wrote:
> I would like to ask what the difference is between
> "static const int" versus "const int". I mean of course
> the former goes in the static data segment of code
> whereas the latter is pushed on the stack, but since
> a const does not change it might as well be declared
> static, but to what advantage.
>
> What I am asking is how to decide whether to use
> "static const int" versus "const int".


In what scope? It matters a lot.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
John Goche
Guest
Posts: n/a
 
      11-11-2011
On Nov 11, 6:31*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 11/11/2011 11:39 AM, John Goche wrote:
>
> > I would like to ask what the difference is between
> > "static const int" versus "const int". I mean of course
> > the former goes in the static data segment of code
> > whereas the latter is pushed on the stack, but since
> > a const does not change it might as well be declared
> > static, but to what advantage.

>
> > What I am asking is how to decide whether to use
> > "static const int" versus "const int".

>
> In what scope? *It matters a lot.


I forgot to mention: inside the block scope
of the body of a function.

Also, as a separate question, what's wrong with

const int foo = 1;

const int bar = foo + 1;

The compiler replies with: error: initializer element is not constant

Of course I could use the preprocessor, but the preprocessor is
not very popular in C++ as it is in C, so what should I do, compose
a function:

const int bar() {
return foo + 1;
}

Seems silly to me.

Thanks,

John Goche
 
Reply With Quote
 
John Goche
Guest
Posts: n/a
 
      11-11-2011
On Nov 11, 7:39*pm, Leigh Johnston <(E-Mail Removed)> wrote:

> > const int foo = 1;

>
> > const int bar = foo + 1;

>
> > The compiler replies with: error: initializer element is not constant

>
> That code is not ill-formed so you are either a) lying or b) have a
> broken compiler.
>
> /Leigh


In this case the code was declared outside of any function as a
global variable, and then I get the error, otherwise you are right
it would compile fine inside a function.

Anyways, I still don't have an answer to the two questions I posted.

Thank you for your replies though,

Regards,

John Goche
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      11-11-2011
On 11/11/2011 1:30 PM, John Goche wrote:
> On Nov 11, 6:31 pm, Victor Bazarov<(E-Mail Removed)> wrote:
>> On 11/11/2011 11:39 AM, John Goche wrote:
>>
>>> I would like to ask what the difference is between
>>> "static const int" versus "const int". I mean of course
>>> the former goes in the static data segment of code
>>> whereas the latter is pushed on the stack, but since
>>> a const does not change it might as well be declared
>>> static, but to what advantage.

>>
>>> What I am asking is how to decide whether to use
>>> "static const int" versus "const int".

>>
>> In what scope? It matters a lot.

>
> I forgot to mention: inside the block scope
> of the body of a function.


There is no difference from the behavior POV. Code-generation-wise, an
automatic constant can easily be replaced by the compiler with its value
when used in an expression (so no storage is usually wasted on it at
all), and a static object will cause some storage allocation and
possibly the use of that storage when the variable is used in an
expression. Should you care about that? I don't know.

> [..]


V
--
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
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const string *Item::getTitle() versus const string Item::getTitle() Ook C++ 8 03-17-2007 03:34 PM
const int versus #define - does "const int" take up space in exe? ThazKool C++ 1 06-11-2006 04:55 AM



Advertisments