Boolean type __c++ in C + +

Source: Internet
Author: User
A Boolean type object can be given literal value true or false, and the corresponding relationship is the concept of true and false.

The method we usually use is to use him to judge the true and false conditions, such as the following code:

#include <iostream>
using namespace Std;

void Main (void)
{
BOOL found = true;
if (found)
{
cout << "found condition is true!" << Endl;
}

}

But some people who are not clear about the concept of a Boolean type can also be considered an object of an integer type, but it cannot be declared as Signed,unsigned,short long, if you generate (short bool found=false;), This will result in a compilation error.

This is the concept of an integer type:

When an expression requires an arithmetic value, the Boolean type object will be implicitly converted to the int type, which is the Shape object, and False is 0,true is 1, see the code below.

#include <iostream>
#include <string>
using namespace Std;

void Main (void)
{
BOOL found = true;
int a = 1;
cout << A + found << Endl;
Cin.get ();
}

A+found Such an expression is established, the output value of 2 for the addition operation.

So when it comes to this, a lot of people ask the pointers. The answer is yes. Such a concept is also valid for pointers, and let's take a look at an example of using an shaping pointer object as a Boolean object:

#include <iostream>
using namespace Std;

void Main (void)
{
int a = 1;
int *pi;
pi=&a;

if (*PI)
{
cout << "*pi is true" << Endl;
}
Cin.get ();
}

The *PI in the code above makes a Boolean-type conversion of the implicit pattern to mean true.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.