c++ - C++中是否有 "normal"一元逻辑运算符

标签 c++ operators logical-operators unary-operator

我的意思是,我们都知道有否定逻辑运算符 !,它可以这样使用:

class Foo
{
public:
    bool operator!() { /* implementation */ }
};

int main()
{
    Foo f;
    if (!f)
        // Do Something
}

是否有任何运营商允许这样做:

if (f)
    // Do Something

我知道这可能不重要,但只是想知道!

最佳答案

您可以声明和定义operator bool() 以隐式转换为bool,如果您是 careful .

或者写:

if (!!f)
   // Do something

关于c++ - C++中是否有 "normal"一元逻辑运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8955206/

相关文章:

mysql - MySQL中where条件中运算符的解析顺序是什么(例如: SELF join)

python - "x not in"与 "not x in"

java - 0xaa 和 0x55 在做什么?

c++ - 双重否定以检查 not NULL

python - 逻辑或列

c++ - 当插入太多元素时,std::map 会自动调整大小吗?

c++ - 在 std::string 上使用 _T

C++模板矩阵类——方阵特化

c - 条件运算符的执行

c++ - 打印字节的正确类型转换