简体   繁体   English

这个bool操作符在C ++中实际上做了什么? 这个奇怪的东西怎么有用呢?

[英]What does this bool operator actually do in C++? And how does this weird thing can be useful?

When I read C++ book I encountered with a strange operator !(): 当我读到C ++书时,我遇到了一个奇怪的操作符!():

class myClass
{ 
     public:
bool operator !() const {}//What is it??
  };

Can someone explain me its purpose please. 有人可以解释一下我的目的吗。

That is an ill-formed program, taken that operator! 这是一个不正确的计划,采取该operator! is declared to return bool but does not return anything. 声明返回bool但不返回任何内容。 With that being fixed, it is the negation operator and it can be called on an object by prefixing ! 修复后,它是否定运算符,可以通过前缀在对象上调用它! .

myClass c;
!c;         // c.operator!()

You can use it like this: 你可以像这样使用它:

myClass some_obj;
...
if (!some_obj) {
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM