Member boolean inconsistency

closed account (1yvXoG1T)
I have a boolean flag that is being set to one value within the class then when it's being read in a different function that value changes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Foo {
    public:
        Foo(int num);
        void Bar();
        bool flag;
};

Foo::Foo(int num) {
    if(num > 1)
        flag = true;
    else
        flag = false;
}

void
Foo::Bar() {
    if(flag == true)
        cout << "Flag is true" << endl;
    else
        cout << "Flag is false" << endl;
}

int main() {
    Foo phoo(5);
    phoo.Bar();
}

Output:
Flag is false


I don't understand how that can happen.

EDIT: Fixed the lack of an object construction as pointed out by Duoas
Last edited on
That shouldn't compile at all, since line 25 is trying to use a non-static member function without constructing an object.

Try this:
23
24
25
26
int main() {
    Foo fooey(5);
    fooey.Bar();
}

Hope this helps.

[edit] BTW, notice how your line 24 creates a temporary object, which does nothing but die before line 25 happens.
Last edited on
closed account (1yvXoG1T)
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.
Did you try the code I gave you? (You didn't.)

Try it, then see what is different between my main() and your main().

Good luck!
closed account (1yvXoG1T)
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();
}
I run this and get "Flag is true".
closed account (1yvXoG1T)
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.
closed account (1yvXoG1T)
@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

EDIT: Added reply to xephon
Last edited on
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.
Last edited on
closed account (1yvXoG1T)
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"
I suspect you've got a loose memory access or something instead. Can you post your whole code which reproduces the error?
closed account (1yvXoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Level::Level(sf::RenderWindow &Win, int argc, char* argv[]) {
    App = &Win;
    char brickCheck;
    std::string fileTemp;

    int x = 0;
    int y = 0;

    if(argc > 1) {
        filename = argv[1];
        newFile = false;
    }
    else {
        filename = "empty.lvl";
        newFile = true;
    }

    fileTemp = filename;

    std::fstream fbin(filename.c_str(), std::ios::binary | std::ios::in);
    if(!fbin) {
        std::cout << "Error: could not open Level file." << std::endl;
        exit(1);
    }

    while(!fbin.eof())
    {
        fbin.read(reinterpret_cast<char*>(&brickCheck), sizeof(char));
        if(!fbin.eof())
        {
            if(brickCheck != '\n' && brickCheck != ' ')
            {
                if(atoi(&brickCheck) < 10 && atoi(&brickCheck) >= 0)
                {
                    layout[x][y] = new Brick(x, y, atoi(&brickCheck));
                    x++;                                              
                    if(x > WIDTH)
                    {
                        x = 0;
                        y++;
                    }
                }
            }
        }
    }

    filename.empty();
    filename = fileTemp;

    fbin.close();
}


I did find out that it's happening while I'm creating the 10th Brick.
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.
Last edited on
closed account (1yvXoG1T)
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++) {
    static int 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.
Topic archived. No new replies allowed.