I have this simple class with a bool and a getter/setter for it:
tile.h
class Tile {
public:
Tile();
virtual ~Tile();
bool isActive() const { return isActive_; };
void setActive(bool status) { isActive_ = status; };
private:
bool isActive_;
};
tile.cpp
#include "tile.h"
Tile::Tile() : isActive_(false) {
}
Tile::~Tile() {
}
Now, when I create a class object in my main function and access the isActive_ bool with the isActive() function, it prints it correctly.
But when I use the setActive function after creating the tile and then print out the status like above, it doesn't change the isActive_ variable and stays always on the value I assigned in the Tile constructor.
example main.cpp
#include "tile.h"
#include <string>
#include <iostream>
int main() {
Tile tile;
// will print the correct thing, based on what I set in the constructor
if (tile.isActive()) {
std::cout << "is active" << std::endl;
} else {
std::cout << "is NOT active" << std::endl;
}
tile.setActive(true);
// will NOT print the correct thing. it will print what I set in its constructor
if (tile.isActive()) {
std::cout << "is active" << std::endl;
} else {
std::cout << "is NOT active" << std::endl;
}
return 0;
}
I think I'm going crazy and I can't for the life of me figure out what I'm doing wrong???
Also, while I'm at it, is there a difference between calling (tile.isActive()) and (tile.isActive() == true) because in the past when the former didn't work, the latter did and I have no idea why. By the way, in this example neither of the two above work :(
EDIT:
You are right, it indeed works. Thank you so much for your replies!
I was just being stupid. Thanks <3