Skip to main content
gamedev.net
🔒 Locked

static const

Started by bctorvik 4 replies 550+ views
Original Post
bctorvik
bctorvik
When do you initialize static const ints? I have class A { static const int a=0; //this is what I would static const int b=1; //*like* to do ... } The compiler complains about this, though "Thinking in C++" seems to think this code is fine (an otherwise great book, btw). The static is causing problems, but it''s definitely what I want. I could just #define, but it''s the principle of the matter... Anyone know how to do this? Thanks, brett
"Me lose brain cells?" laughter, then a pause "Why I laugh?"
DrPizza
DrPizza
If you''re using VC++6, it has a known bug that prevents this. If you search the KB you can find a list of known bugs.

Workarounds including using an enum for the values instead.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
bctorvik
bctorvik
Ah, thanks -- I''ll check it out.
"Me lose brain cells?" laughter, then a pause "Why I laugh?"
cody
cody
your header:

class A {
static const int a=0;
static const int b=1;
};

your cpp-file:

const int A::a=0;
const int B::b=1;

(if this doenst work try without const in the cpp file, i dont know exactly)
bctorvik
bctorvik
The enum-hack worked great. Just did this (to those who might want to know) ..

Class A {
enum { var=100 };

...

}

And var acts like a ''static const'' variable.

"Me lose brain cells?" laughter, then a pause "Why I laugh?"
donmc
donmc
cody has the right idea:

  

class A
{
static const int a;
static const int b;
};

now outside of any objects:

const int A::a = 0;
const int A::b = 1;

a and b are allocated and given values when your program first starts (before any object instantiation). Both a and b are const and static just as you wanted. :)



cheers,
don

"They that can give up essential liberty to obtain temporary safety deserve neither liberty nor safety."
- Benjamin Franklin

Edited by - donmc on December 27, 2001 8:35:04 PM
"They that can give up essential liberty to obtain temporary safety deserve neither liberty nor safety."- Benjamin Franklin

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.