Objectmix
Tags Register Mark Forums Read

Is 'const int' same as 'static const int' ? : c++

This is a discussion on Is 'const int' same as 'static const int' ? within the c++ forums in Programming Languages category; Is 'const int' same as 'static const int' ? These variables are declared inside a class....


Object Mix > Programming Languages > c++ > Is 'const int' same as 'static const int' ?

c++ comp.lang.c++ usenet archive

Reply

 

LinkBack Thread Tools
  #1  
Old 06-03-2009, 12:05 AM
Junior Member
 
Join Date: Jun 2009
Posts: 1
lakshmi is on a distinguished road
Default Is 'const int' same as 'static const int' ?

Is 'const int' same as 'static const int' ?
These variables are declared inside a class.
  #2  
Old 07-31-2009, 05:43 PM
Junior Member
 
Join Date: Jul 2009
Posts: 4
dorcsssc is on a distinguished road
Default Re: Is 'const int' same as 'static const int' ?

Nope.

By "inside a class" I hope you meant inside the scope of a declaration of a class. Assuming that's what you meant...

In such a context, static declares that there will be only 1 such class-wide data member; as opposed to there being 1 per-instance (inside every object of the class). The const declares that this variable will receive a value only during its construction.

Inside a member function of a class (or for that matter, any function), static means that it's non-automatic; that is, that its value survives returning from the function (also that it can have a value prior to ever entering it, too). It also means that multiple threads of execution through such a routine can contend over access to the variable.

Even outside the body of a function, static const int is not necessarily the same as const int. At the compiler's discretion, a simple const declaration doesn't necessarily reserve a storage location; it's just a symbolic literal declaration similar to a #define manifest constant, except that it now has an actual hard type, and that it can't be #undef'd later on. In contrast, static const int definitely reserves an actual storage location that's private to that compilation unit (and you'll have to initialize it, because you can't later on - it's const, right?). Note that in the latter case, const is used as a modifer - not a declarator: we're declaring a variable that cannot be modified, whereas earlier, we were declaring a symbolic constant.

Sadly (and by now obviously), static and const are both over-used keywords in C and C++; their precise meaning depends on where its used. Crack the books, I suggest. Hope this helped some.
Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
const correctness - should C++ prefer const member over non-const? usenet c++ 19 10-31-2008 08:56 AM
Casting int'** to 'const int * const * const' dosn't work, why? usenet C 0 06-01-2007 02:02 AM
'int' not serializable!? usenet Weblogic 4 10-31-2006 06:57 PM
i=infinity;0= i*sin k*pi, 1=cos k*pi, k=m/n, n=4,m=0-00; c*G=20=const, 1/sgrt2>G>0.5, 6<N = NA ^2surf/NAvol<7 ; h/N =11=const, e+i*pi; D universe =f(h)*1/ (a))^4, T=f( m, S, D) usenet Theory 0 08-08-2006 07:13 AM
'int' is not Serializable usenet Weblogic 0 08-17-2005 11:08 AM


All times are GMT -5. The time now is 08:44 AM.

Managed by Infnx Pvt Ltd.