删除按位标志

时间:2017-04-16 10:37:54

标签: c++

如何从枚举中删除标记

我可以使用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));
}

这是我试图添加/删除

的功能
void Window::SetButton(WindowButton button, bool show)
{
    if (show)
        m_Buttons |= (button);
    else
        m_Buttons | ~(button); // This is not working to remove flags
}

1 个答案:

答案 0 :(得分:4)

  

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

当然不是。它设置应该清除的位置,然后丢弃结果。应该是

m_Buttons &= ~button;

NB括号在这里是多余的。