I've been trying to use const global variables but they never seem to be initialized even after main() has begun executing.
I'm trying to create bit masks using std::bitset because i want bit masks larger than 32 bits, which an enum doesn't seem to allow. So my globals look something like this.
But these always seem to be set to zero whenever i try to actually use them.
"main.cpp"
1 2 3 4 5 6 7 8 9
#include "Bitmask.h"
int main()
{
BitMask test = MASK_THREE; //This should set the third bit to 1 but it dosn't, all bits are 0.
test = BitMask(1 << 2); //This works, the third bit is now set to 1.
return 0;
}
I don't understand why they are not initialized even after main() has begun.
I've read you can force initialization if you change the global variable into a static variable within a global function, the function initializes the variable on first use then just returns a reference to the variable.
But that would mean writing a function for every variable, also i'm likely to be using these bit masks hundreds, if not thousands of times a second, wouldn't the function calls effect performance? Isn't there another way?
Sorry for the verbose explanation but i wanted to explain exactly what i'm doing even if the problem has nothing to do with bitsets.
I just created a new project and tried the code samples and it works perfectly, but in my current project the initialize strings are never called so it's like the cpp file is completely ignored, i guess that means there's something wrong in my current project? There's only one cpp file that defines the masks.
Copy initialise a BitMask object: const BitMask MASK_ONE = <some expressioin that evaluates to a BitMask object>
Sequencing (comma) operator (non-overloaded): expression1 , expression2
Evaluation and side effects of expression1 is sequenced before evaluation and side effects of expression2. expression1 is evaluated and its value is discarded.
The type and value of the result of expression1 , expression2 is the type and value of expression2
( ( std::cout << "initialise MASK_ONE\n" ), BitMask(1) )
first evaluates ( std::cout << "initialise MASK_ONE\n" ) and discards the result.
then evaluates BitMask(1) which is the result of the expression
The bitmask is initialised with const BitMask MASK_ONE = BitMask(1) ;
The precedence of the sequencing operator is low; hence the parentheses around the expressions..
EDIT:
Any expression, the evaluation of which will print out the message and yield the appropriate result would do.
You may find this easier on the eye:
Thanks a bunch JLBorges, I actually just found out whats causing the problem. In the Visual Studio settings on my current project i have set the entry point to main(), but in the default test project i set up there is no set entry point, when i remove that setting the globals get initialized.
The reason i set the entry point to main() in the first place was because there was a conflict with a library i'm using, but now i've removed the setting there doesn't seem to be any conflict, weird.
So thanks again, and thanks for teaching me the sequencing operator.