Jump to content
  • Advertisement
Sign in to follow this  

problem with "bool" as class variable

This topic is 3682 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!