C++ Implicit Conversions to boolean

Introduction

Any built-in type can be converted to boolean.

For objects of those types, any value other than 0, gets converted to a boolean value of true, and values equal to 0, implicitly convert to a value of false.

Example:

int main() /*  w  w  w  .  ja  v a2  s. c  om*/
{ 
    char mychar = 64; 
    int myint = 0; 
    double mydouble = 3.14; 
    bool myboolean = true; 

    myboolean = mychar;    // true 
    myboolean = myint;     // false 
    myboolean = mydouble;  // true 
} 

Conversely, a boolean type can be converted to int.

The value of true converts to integer value 1 and the value of false converts to integer value of 0.




PreviousNext

Related