使用布尔重新present整数 [英] using Boolean to represent integers

查看:141
本文介绍了使用布尔重新present整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,布尔用于重新present布尔。那就是它拥有真或假。但在某些情况下,我们可以使用布尔重新present整数也。
是什么的布尔的含义= 5; 在C ++


解决方案

  

是什么 BOOL 1 = 5的含义; 在C + +


它实际上等同于写

 布尔A =(5!= 0);


  

但在某些情况下,我们可以使用布尔重新present整数也。


不是真的。在布尔值只的再presents 的是否使用整数初始化为零( - > )或不( - > 真正


倒过来(如<一提到href=\"http://stackoverflow.com/questions/30332082/using-boolean-to-re$p$psent-integers/30332119?noredirect=1#comment48759151_30332119\">@Daniel弗雷的评论)将被转换回一个整数 0 真正将成为 1 。结果
所以原来的整数值(或任何其他前pression结果看上去是指针,除了 nullptr 双击值不完全重新presenting 0.0 )都将丢失。


结论

LRiO的回答提到的,这是不可能的存储等信息比真正布尔变量。

有保证的转换规则,但(引用自 CP preference.com ):


  

的安全问题,BOOL


  
  

直到C ++ 11引进显式转换功能,设计一个类,应该在布尔上下文中使用的(如如果(OBJ){...} )presented一个问题:给定一个用户定义的转换功能,如 T ::布尔运算符()const的; ,隐式转换序列允许一个额外的标准转换&LT;该函数调用,这意味着最终布尔可转换为int,允许此类code作为 OBJ℃之后序列; 1; INT I = OBJ;


  
  

这个问题的一个解决方案,早期可以在的std :: basic_ios ,它定义了运营商!,看到运营商的void * (直到C ++ 11),这样,code,如如果(的std :: CIN){...} 编译,因为无效* 可转换为布尔,但 INTñ =性病::法院; 不编译,因为无效* 是无法转换为 INT 。它仍然允许废话code,如删除性病::法院; 编译,许多pre-C ++ 11的第三方库设计与更复杂的解决方案,被称为安全布尔成语


In c++, bool is used to represent Boolean. that is it holds true or false. But in some case we can use bool to represent integers also. what is the meaning of bool a=5; in c++?

解决方案

"what is the meaning of bool a=5; in c++?"

It's actually equivalent to writing

bool a = (5 != 0);

"But in some case we can use bool to represent integers also."

Not really. The bool value only represents whether the integer used to initialize it was zero (-> false) or not (-> true).


The other way round (as mentioned in @Daniel Frey's comment) false will be converted back to an integer 0, and true will become 1.
So the original integer value's (or any other expression results like pointers, besides nullptr, or double values not exactly representing 0.0) will be lost.


Conclusion

As mentioned in LRiO's answer, it's not possible to store information other than false or true in a bool variable.

There are guaranteed rules of conversion though (citation from cppreference.com):

The safe bool problem

Until the introduction of explicit conversion functions in C++11, designing a class that should be usable in boolean contexts (e.g. if(obj) { ... }) presented a problem: given a user-defined conversion function, such as T::operator bool() const;, the implicit conversion sequence allowed one additional standard conversion sequence after that function call, which means the resultant bool could be converted to int, allowing such code as obj << 1; or int i = obj;.

One early solution for this can be seen in std::basic_ios, which defines operator! and operator void* (until C++11), so that the code such as if(std::cin) {...} compiles because void* is convertible to bool, but int n = std::cout; does not compile because void* is not convertible to int. This still allows nonsense code such as delete std::cout; to compile, and many pre-C++11 third party libraries were designed with a more elaborate solution, known as the Safe Bool idiom.

这篇关于使用布尔重新present整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆