4

I'm planning to have two bool class members (m_alive & m_dead) so that their values are always opposite ones. This might seem something stupid (in fact, it may just be stupid), but as you can see in the code below, but what I really look for is to have a clearer way to check the object status depending of the circumstances, and it would be useful not having to type !m_alive or !m_dead. So, yeah, I don't really need both members but I can't think of an easier way of doing this.

The first idea that came to my mind is to create a function that changes the state of one of them if the other one changes too, but I'm pretty sure there needs to be a simpler, easier and faster way of keeping each one with it's correct value.

AFAIK, it's not possible to do it with define's since there would be as many of them as different objects I plan to create and doesn't seem practical.

#define object1.m_death= !object1.m_alive;
#define object2.m_death= !object2.m_alive;
// ...

So here you have the main idea of having both members:

class myclass
{
    public:
    // (...)
    private:
        bool m_alive;
        bool m_death;   // Always !m_alive
};

int main()
{
    myclass myobject;
    // (...)
    if (myobject.m_dead)
    //...
    if (myobject.m_alive)   // clearer than !myobject.m_dead()
    // ...
}

Any suggestions of how-to keep'em updated is welcome, as well as any other ways of implementing my idea. Thanks in advance!

Eduardo

PD: While re-reading my question, enumerated types have just came to my mind.

It would emply checking myobject.m_status==dead or myobject.m_status==alive, being dead and alive possible values of dead_or_alive enum-type class member dead_or_alive m_status.

Could this be a nicer approach to what I'm seeking, despite being a bit longer syntax?


Final edit:

Thanks to all who commented and answered. Here's the solution I've finally adopted:

enum Piece_status:bool{     dead= false,    alive= true};

class Piece
{
    public:
        bool isAlive() const    {return  m_status;}
        bool isDead()  const    {return !m_status;}
    protected:      
        Piece_status        m_status;   // dead=false, alive=true
};
eduherminio
  • 1,514
  • 1
  • 15
  • 31
  • 6
    They are private. So you code won't compile. And you should add a query to your class instead of adding redundant fields. `myobject.is_dead()` or `myobject.is_alive()` can both be implemented in terms of a single `bool`. – StoryTeller - Unslander Monica Jan 03 '17 at 13:17
  • 3
    It's redundant and not really needed. `!myobject.m_dead` is just as clear as `myobject.m_alive`. – Some programmer dude Jan 03 '17 at 13:17
  • 2
    two methods: is_alive is_dead and base its implementation in your bool – F.bernal Jan 03 '17 at 13:21
  • It is a good idea to to keep member variables as private; however, you should give them access by `set` and `get` functions which must be `public`. – macroland Jan 03 '17 at 13:23
  • 5
    @macroland If you have a public getter and a public setter, it's basically a public variable. – Bartek Banachewicz Jan 03 '17 at 13:25
  • @macroland - Class member functions should encapsulate behavior. An accessor pair isn't behavior encapsulated. It's a cumbersome way to make an aggregate. – StoryTeller - Unslander Monica Jan 03 '17 at 13:32
  • Thanks all for the comments. I guess this is a nice read: http://stackoverflow.com/questions/14399929/should-i-use-public-or-private-variables – macroland Jan 03 '17 at 13:34
  • You're right, @StoryTeller. Thanks everyone for the comments! – eduherminio Jan 03 '17 at 13:39
  • 2
    There are 2 hard problems in computer programming. Cache invalidation, naming things, and off-by-one errors. Your choice of two variables leads you to having a "cache" of the value (am I alive or dead?) in two spots. Don't make hard problems for yourself. – Yakk - Adam Nevraumont Jan 03 '17 at 14:25

3 Answers3

10
class myclass
{
    public:
        bool isAlive() const { return m_alive; }
        bool isDead() const { return !m_alive; }
    private:
        bool m_alive;
};

int main()
{
    myclass myobject;
    // (...)
    if (myobject.isDead())
    //...
    if (myobject.isAlive())
    // ...
}
F.bernal
  • 2,594
  • 2
  • 22
  • 27
6

You're trying to violate the "Single Source of Truth" best practice, by keeping 2 copies of the same information.

If you're looking for clarity (and you don't find if(m_alive) and if(!m_alive) clear enough) then add custom getters to myclass:

bool isAlive() const { return m_alive; }
bool isDead() const { return !m_alive; }
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Nice to know the name of the best practice so that not to be tempted to violating it again, thanks! And also for the answer, just what I was loooking for! – eduherminio Jan 03 '17 at 13:45
1

You can implement this by using a setter for changing the variables:

// optional setters
void setDead() { setDead(true); }
void setAlive() { setDead(false); }

// main setter
void setDead(bool isDead) {
    m_alive = !(m_dead = isDead);
}
MaanooAk
  • 2,418
  • 17
  • 28