2017-04-16 56 views
-1

我如何从我的枚举删除标志删除逐位标志

我可以添加他们很容易与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 
} 
+0

使用'|'而不是'| ='意味着'm_Buttons'不会被修改......但是'| ='也不是正确的。 – hvd

+1

提示:在“flags | = f”上转换一个位标记。关闭一个bitflag:'flags&=〜f'。 –

回答

4

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

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

m_Buttons &= ~button; 

NB括号在这里是多余的。

+0

'&=':找不到操作符,它需要一个类型为 – xuzisijuya

+1

@xuzisijuya的左操作数因此,定义'&='与定义'| ='相同的方式,除了使用'&'。 – hvd

+1

@xuzisijuya(我几乎忘了''operator〜'以及:) – hvd