【 tulaoshi.com - 编程语言 】
布尔类型对象可以被赋予文字值true或者false,所对应的关系就是真与假的概念。
我们通常使用的方法是利用他来判定条件的真与假,例如下面的代码:
![]()
![]()
![]()
![]()
!-- frame contents -- !-- /frame contents --
![]()
![]()
![]()
![]()
#include iostream
using namespace std;
void main(void)
{
bool found = true;
if (found)
{
cout "found条件为真!" endl;
}
} 但是一些概念不清的人却不知道布尔类型的对象也可以被看做是一种整数类型的对象,但是他不能被声明成signed,unsigned,short long,假如你生成(short bool found=false;),那么将会导致编译错误。
其为整数类型的概念是这样的: 当表达式需要一个算术值的时候,布尔类型对象将被隐式的转换成int类型也就是整形对象, false就是0,true就是1,请看下面的代码! #include iostream
#include string
using namespace std;
void main(void)
{
bool found = true;
int a = 1;
cout a + found endl;
cin.get();
} a+found 这样的表达式样是成立的,输出后的值为2进行了加法运算!
那么说到这里很多人会问指针也可以吗?回答是肯定的这样一个概念对于指针同样也是有效的,下面我们来看一个将整形指针对象当作布尔对象进行使用的例子:#include iostream
using namespace std;
void main(void)
{
int a = 1;
int *pi;
pi=&a;
if (*pi)
{
cout "*pi为真" endl;
}
cin.get();
} 上面代码中的*pi进行了隐式样的布尔类型转换表示为了真也就是true。
![](http://img.warting.com/allimg/2010/c1215/129242WR410-59140.jpg)
更多内容请看C/C++技术学堂 C/C++技术专题 Solaris基础知识入门专题,或