-1

How would i remove flags from my enum

I can add them easy with m_Buttons | (button);

enum WindowButton
{
    None = 0,

    Minimize = (1 << 0),
    Maximize = (1 << 1),
    Close = (1 << 2),
};
inline WindowButton operator|(WindowButton a, WindowButton b)
{
    return static_cast<WindowButton>(static_cast<int>(a) | static_cast<int>(b));
}
inline WindowButton& operator |= (WindowButton& lhs, WindowButton rhs)
{
    return lhs = static_cast<WindowButton>(static_cast<WindowButton>(lhs) | static_cast<WindowButton>(rhs));
}

This is the function where im trying to add / remove

void Window::SetButton(WindowButton button, bool show)
{
    if (show)
        m_Buttons |= (button);
    else
        m_Buttons | ~(button); // This is not working to remove flags
}
xuzisijuya
  • 15
  • 5
  • Using `|` instead of `|=` means `m_Buttons` isn't going to be modified... but `|=` wouldn't be right either. –  Apr 16 '17 at 10:40
  • 1
    Hint: Turning a bitflag on `flags |= f`. Turning a bitflag off: `flags &= ~f`. – Nik Bougalis Apr 16 '17 at 10:40

1 Answers1

4

m_Buttons | ~(button); // This is not working to remove flags

Of course not. It sets where it should clear, and then throws away the result. Should be

m_Buttons &= ~button;

NB parentheses are redundant here.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
user207421
  • 305,947
  • 44
  • 307
  • 483