problem with "bool" as class variable

Started by
30 comments, last by Zahlman 15 years, 12 months ago
hello, i have the following class definition

#pragma once

#include <windows.h>
#include "glew.h"
#include "texture.h"


class button
{
	public:
		button() {};
		~button() {};
		void init(int xlu, int ylu, int xro, int yro);
		void render();
		void setTexture(texture* tex);
		
		float transparency;
		int xmin;
		int xmax;
		int ymin;
		int ymax;
		bool mouseover;
		bool mousedown;
		bool active;		

	private:
		texture* tex;
};

the problem are the three bool variables. they always seem to messup the following variables. in this case it is the tex variable (becomes nullpointer). if i move the bools up before the trasparency for example, this one is messed up (contains wrong values). if i change the bool to BOOL (notice the uppercase) which is defined as int by windef.h everything seems to work fine. what is the problem here and why cant i use normal c++ bools? thanks!
Advertisement
Define 'messed up'....

How are you using it, and from what point are the values messed?

I notice your constructor doesnt initialise the values at all so when are they all set?
thank you,

they are actually set in the init-function, but even if i initilaize them in the constructor nothing changes.

by messed up i mean as i have said above. if it is a pointer i get a reading-access-violation although i have definitely set the pointer before and for the primitve types such as float or int they contain random values.
More than likely, you are either not initializing them (as Benden said) or you have memory corruption going on. Post the constructor and the code that writes to the tex variable.
thank you, but even if i initialize them in the constructor like this, the problem persits.
		button() {			transparency = 0.5;			mouseover = false;			mousedown = false;			active = false;			xmin = 0;			xmax = 0;			ymin = 0;			ymax = 0;		};


what is really strange about this, is as i wrote above, as soon as i change the bool to BOOL everything works fine.

thanks!
So just put a data breakpoint on one of the other variables that gets messed up and see what's going wrong.
Quote:Original post by ehmdjii
thank you, but even if i initialize them in the constructor like this, the problem persits.
*** Source Snippet Removed ***

what is really strange about this, is as i wrote above, as soon as i change the bool to BOOL everything works fine.

thanks!


Quick couple of comments, use the initialiser list rather than setting them in the constructor body like that. Also its best to init them in order of definition in the class. Remember for completeness to init tex to NULL as it is a pointer.

Once you've done all that stick a breakpoint just after you create an instance of your button class and check all the member vars. You are checking them in debug and not release arent you?
Quote:Original post by ehmdjii
what is really strange about this, is as i wrote above, as soon as i change the bool to BOOL everything works fine.


I guarantee you that, though changing to BOOL may fix these specific *symptoms* of the problem, it will not fix the problem itself. Like others have said, step through your code with a debugger and especially watch where you access the pointer tex. If it is a dynamic array, it is likely that you are corrupting your other members through it.
The problem doesn't lie in the code you've posted so far; it's elsewhere. This isn't surprising since you have public data members and an init function which is separate from the constructor.
thank you everyong for your help.

yes it seems that the problem is elsewhere, cause it looks like the instance that is accessed later on when the problem occurs, is a complete different one.

but how can this happen? how can an instance be created without passing through the constructor?

i am strong the instances of the button class in an array and access them only via this array:
button b0, b1, b2, b3, b4, b5, b6, b7;
button buttonArray[8] = {b0, b1, b2, b3, b4, b5, b6, b7};


Benden, thanks for your adivces. i am now initializing also the tex-pointer to NULL. yes, i am debugging in DEBUG mode.

Simian Man, thanks. yes i am going through the code step by step but i still couldnt find the point where the data gets corrupted.

This topic is closed to new replies.

Advertisement