Local constant problems

Started by
6 comments, last by SiCrane 18 years, 8 months ago
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?
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
Advertisement
//globals.hextern thing some_global;//globals.cppthing some_global = initial_value;


but you could look here to read about whats wrong with globals
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
In C++, if it's a constant you can just put:

const SOME_CONST = 5;

in a header.
Quote:Original post by SiCrane
In C++, if it's a constant you can just put:

const SOME_CONST = 5;

in a header.


as long as it's of built-in type.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.
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.
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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.

This topic is closed to new replies.

Advertisement