I have a class named Colorblind
which has getter and setter methods for a boolean variable called bool toggleColorBlind = false
. I have multiple other classes such as Menu
which a user can toggle the boolean variable and set it to true.
When I try to get the variable from another class, like Game
, the boolean variable resets to its initial value when I instantiate the class and use the getToggleColorBlind()
method within the class.
What can I do to prevent the boolean from resetting to its initial value and keep track of it when the class is instantiated and used? Could this be done a better way by using static
or something?
Code example:
#include "Colorblind.h"
bool Colorblind::getToggleColorBlind()
{
return mToggleColorBlind;
}
void Colorblind::setToggleColorBlind(bool state)
{
mToggleColorBlind = state;
}
class Colorblind
{
public:
Colorblind();
bool toggleColorBlind(){return mToggleColor;}
bool getToggleColorBlind();
void setToggleColorBlind(bool state);
private:
bool mToggleColorBlind = false;
};
Now the Menu is where a user can enable/disable colorblind mode which works as expected.
Colorblind colorblind;
while (std::getline(cin, command)) {
if (command == "toggle colorblind") {
bool isToggled = colorblind.getToggleColorBlind();
if (isToggled == true) {
colorblind.setToggleColorBlind(false);
} else {
colorblind.setToggleColorBlind(true);
}
{
}
Now my problem is when I try to access the mToggleColorBlind
by doing Colorblind colorblind; colorblind.getToggleColorBlind()
from any class such as Game
to set colors etc.. The value is lost, how can I keep track of this.