Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping two bool class members with opposite values

Tags:

c++

class

c++11

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
};
like image 668
eduherminio Avatar asked Dec 03 '22 13:12

eduherminio


2 Answers

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())
    // ...
}
like image 177
F.bernal Avatar answered Dec 28 '22 14:12

F.bernal


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; }
like image 35
Jonathan Mee Avatar answered Dec 28 '22 14:12

Jonathan Mee