int还是bool? [英] int or bool?

查看:108
本文介绍了int还是bool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么将bool类型引入到c ++中?

它提供了什么,int不是,并且两个完全在条件表达式中可以互换?



谢谢

Fred

解决方案

Fred写道:

为什么bool类型被引入到c ++中?
它提供了什么,int没有,并且两个完全可以在条件表达式中互换?




首先,我喜欢bool,因为它让我说出我的意思。如果我想要

一个函数来返回一个真的或假价值,我让它返回bool,

,因为这正是我的意思。


它们完全可以互换,因为1!= - 1,但是bool (1)== bool(-1)。

Bools可以在很多方面被认为几乎与相同。在你的地方,你只需要允许0或1美元,但是他们强加给你这个条件而不是强迫你确保你自己申请。


您当然可以编写具有不同行为的函数和模板

,具体取决于给出的是哪一个,但当然这对于

都是如此类型。


克里斯


" Fred" <星期五** @ somewhere.abc>在消息中写道

news:cp ********** @ titan.btinternet.com ...

为什么将bool类型引入c ++?
参见例如:
http:// www。 gotw.ca/gotw/026.htm

它提供了什么,int不是,并且两个完全可以在条件表达式中互换?



bool只有两个可能的值/状态:true和false。


表达式如!= b有不同的含义

for bool比int更好。

当a和b是bool类型时,它相当于一个逻辑x或。


每当一个人应该使用bool值只能是真或假,

就像一个人应该使用int而不是double,因为它有助于避免

错误。


hth,

Ivan

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++<> ; http://www.brainbench.com

"佛瑞德" <星期五** @ somewhere.abc>在消息中写道

新闻:cp ********** @ titan.btinternet.com ...

为什么是bool类型介绍到c ++?


因为为int定义一个自己的同义词并将其用作

布尔类型的做法已经发展到大型系统通常具有的程度

布尔类型的多个不兼容的定义。

它提供了什么,int不是,并且两个完全可以在条件表达式中互换?




它们不可互换。首先,如果int和bool不同

类型,你可以重载它们。


Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?

Thanks
Fred

解决方案

Fred wrote:

Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?



First of all, I like bool because it lets me say what I mean. If I want
a function to return a "true" or "false" value, I make it return bool,
because that is exactly what I mean.

They are totally interchangable, because 1!=-1, but bool(1)==bool(-1).
Bools can in many ways be considered "almost the same as" ints where you
are only allowed 0 or 1, but they impose this condition for you rather
than forcing you to make sure you apply it yourself.

You can of course write functions and templates which behave differently
depending on which one they are given, but of course this is true for
all types.

Chris


"Fred" <Fr**@somewhere.abc> wrote in message
news:cp**********@titan.btinternet.com...

Why was the bool type introduced into c++? See for example:
http://www.gotw.ca/gotw/026.htm
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?


bool only has two possible values/states: true and false.

Expressions such as a != b have a different meaning
for bool than for int.
When a and b are of bool type, it is equivalent to a logical x-or.

One should use bool whenever a value can only be true or false,
just as one should use int rather than double as it helps avoid
bugs.

hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


"Fred" <Fr**@somewhere.abc> wrote in message
news:cp**********@titan.btinternet.com...

Why was the bool type introduced into c++?
Because the practice of defining one''s own synonym for int and using it as a
boolean type had grown to the point where large systems typically had
multiple incompatible definitions of boolean types.
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?



They are not interchangeable. For one thing, if int and bool are different
types, you can overload on them.


这篇关于int还是bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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