Jump to content
  • Advertisement
Sign in to follow this  
warp X

Local constant problems

This topic is 4838 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

I'm coding a simple game with Visual Studio .NET 2003. I want to use global constants (in a namespace, though). I seem to stumble into all kinds of problems trying to use them. The compiler says for example that "error C2065: 'SOMECONST' : undeclared identifier" Does anyone know about any issues with global constants? I think that I just dont know how to declare them or something. Should I use the extern keyword? I have like files GAME_Main.h and GAME_Main.cpp. Where should I declare the constants, assign into them, etc?

Share this post


Link to post
Share on other sites
Advertisement
Nope, any type can be declared as const in a header. Ex:

const std::pair<int, int> const_pair = std::make_pair(5, 7);

While certainly strange, it is legal C++ and will link happily since all const variables have implicit internal linkage.

Share this post


Link to post
Share on other sites
Ok, thanks. Actually, I was hoping to hear there were some issues with global consts in a namespace. That's because I tried them in a small test example and they worked ok, but then when I tried them in my bit larger game project, they didn't work of course, or there were some problems. Well maybe it's just something else than the declaration. I have to isolate the problem somehow.
I appreciate your help.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Nope, any type can be declared as const in a header. Ex:

const std::pair<int, int> const_pair = std::make_pair(5, 7);

While certainly strange, it is legal C++ and will link happily since all const variables have implicit internal linkage.


Strangeness... I wonder where I picked up that missconception then.

Share this post


Link to post
Share on other sites
Possibly because const variables with dynamic initialization can be treated differently by the compiler. For example, an integer may be embedded directly into a read only segment of the executable's image, but a dyanmically initialized constant may be placed into a writable segment of the executable image and intialized at run time. Or maybe because it's inadvisable to stick large user types as a constant in a header. For example making a 3 MB user type a constant in a header would give each translation unit a different copy of that 3 MB object, bloating the app tremendously.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!