Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bctorvik

static const

This topic is 6114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!