gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:46:38 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com