0

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

A. D.
  • 728
  • 1
  • 9
  • 27

2 Answers2

1

is there a difference between calling (tile.isActive()) and (tile.sActive() == true)

For this case - no, there's no difference at all.


because in the past when the former didn't work, the latter did and I have no idea why

because if( something ) is true, if and only if this something is evaluated to true (meaning - different from 0). So, it depends a lot on the specific case.


On the question - try to rebuild your project. Everything is perfectly fine.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • @AurelleDenby - can you share what was the solution? Rebuilding, right? – Kiril Kirov Jan 15 '14 at 15:33
  • Actually, no. The code I typed up in my post wasn't relevant to my problem. The real problem I had is here: http://stackoverflow.com/questions/21141168/why-cant-i-change-objects-in-a-vector I wasn't accessing the objects in the vector that I'm using by reference, so it would just copy them and the original objects stayed the same. It was stupid of me not to post my real and complete problem in the first place but rather this stripped down one ;P I'm sorry – A. D. Jan 15 '14 at 15:44
1

Everything looks good. The only thing I see is that in main.cpp you include "tile" and not "tile.h". Maybe you have an older version of tile.h with the "tile" name?

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51