Sorry, that was actually a typo produced from my fury lol
Everything compiles fine, the only problem is it's sending out the wrong output. I've tested repeatedly to make sure that the flag is being assigned the proper value at construction, however that value changes when it enters the Bar function.
I tried your rendition and get the same output. Perhaps I'm missing something but all I see is a change in name which should have no effect on the operation.
1 2 3 4 5 6
int main() {
Foo phoo(5);
//Foo fooey(5);
phoo.Bar();
//fooey.Bar();
}
You see my confusion. I really don't get it. I seriously just tried this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Foo::Foo() {
int num = 5;
if(num > 1)
flag = true;
else
flag = false;
// Insert code that loads a map, does not use flag at all
if(flag == true)
cout << "Flag is true" << endl;
else
cout << "Flag is false" << endl;
}
And it still printed false. Could something be wrong with my compiler or something?
Have you tried running the code through a debugger? It might shed some light on where exactly the problem is occurring, even if it doesn't help you solve it.
I ran this as well. I got nothing wrong. the output is Flag is true.
I just wanna point out though. as you have Bar() to print out flag, you'd probably want to hide flag from other class or so. then the best way is to implement it as a private member.
@Zhuge
I have and haven't had much luck. But what's peculiar is I did find out that at some point after or during opening my map file the flag takes on the value of 160. I don't get it but I guess I'll just fight with it until I figure out something.
@xephon
I understand that and I actually did implement it as a private member. I just didn't feel it was necessary to say so in my pseudo-code
depend on your compiler, it is possible a public member will be in some public memory address. it is public is because the compiler has not wrapped it up to make sure the class instance ALONG can access that memory. if you map file was copied over this memory location and compiler was not aware, it would be overlapped.
this is theoretically right, though i doubt this is the case. depending on my own limited knowledge, modern C++ compilers should not have this "fatal" flaw as leaving compiler declared memory visible to other programs...
nevertheless, this is the only explanation i can think of right now.
The memory overlap does seem very plausible somehow. About half way through loading in my map (basically, read in from the file and copy object information to an array) than the flag's value changes, even though flag is not even mentioned on that line.
Is there a way that I can completely reserve that memory slot? I know I can make certain "requests"
so the above is the code u used to load the map? if so, i can not make any error from this snippet.
have you tried to declare flag as a const? i'm not quite familiar with const but as i remember, this keywrod prevent the var to be changed after it's initialized.
actually i'm always confused by static, const and final. could someone actually differentiate them for me? thanks.
@neurotrace: sorry to take over ur topic a little bit.
The problem with declaring it a const is that I would have to declare it at the time of the constructor, which would give it local scope and not class scope. If I made it a const in the class declaration then I wouldn't be able to change it's value based on if I loaded in a file or not :/
To my understanding, static essentially makes sure that something is only declared once. Ex:
1 2 3 4
for(int i =0; i < 10; i++) {
staticint counter = 0;
counter++;
}
And counter would end up being equal to 10.
@xephon: Don't worry about it, the more use we can get out of this thread the better lol.