A detailed explanation of the use of C + + Boolean types and references

Source: Internet
Author: User


1. Boolean type


Boolean types in C + +

  • C + + adds to the basic type of language systembool

  • The desirable values in C + + areboolonlytrueandfalse

  • Theoreticallybooloccupies a byte

Attention:

trueRepresents the truth value, which is represented internally by the compiler

falseRepresents a non-truth value, and the compiler internally uses the

In the C language:

Use integer values insteadboolof types, often0:flase, 1:true


C + + does type enhancement, adds a very rigorousbooltype,trueandfalseexists as a keyword.



In C + + Boolean types,booltypes havetrueonlyfalsetwo values, and the C + + compiler converts non-0 values totrue0 values tofalse.


bool b = 0;
printf ("b =% d \ n", b);
b ++;
printf ("b =% d \ n", b);
b = b-3;
printf ("b =% d \ n", b);
// Does the bool type support mathematical operations?
in fact, in the C + + language, the internal implementation of the Boolean type is implemented with a single byte integer, thebooltype supports mathematical operations, the compiler adjusts internally, not 0 is true,0 false


Code test:


#include <stdio.h>

int main (int argc, char * argv [])
{
     bool b = false;
     int a = b;
    
     printf ("sizeof (b) =% d \ n", sizeof (b));
     // sizeof (b) = 1, bool type takes one byte
     printf ("b =% d, a =% d \ n", b, a); 0
     // b = 0, a = 0
    
     b = 3; // b = 1
     a = b; // a = 1
    
     printf ("b =% d, a =% d \ n", b, a);
    
     b = -5; // b = 1
     a = b; // a = 1
    
     printf ("b =% d, a =% d \ n", b, a);
    
     a = 10; // a = 10
     b = a; // b = 1
    
     printf ("a =% d, b =% d \ n", a, b);
    
     a = 0; // a = 0
     b = a; // b = 0
    
     printf ("a =% d, b =% d \ n", a, b);
    
     return 0;
}

The Boolean type is the basic data type in C + +

  • You can defineboola global variable of type

  • boolconstants that can define types

  • You can defineboola pointer to a type

  • boolan array of types can be defined

    ......


2, trinocular operator



The three-mesh operator is upgraded in C + +



Consider whether the following code is correct, compiling the run test in C and C + + environments, respectively


int a = 1;
int b = 2;
(a <b? a: b) = 3;
printf ("a =% d, b =% d \ n", a, b);
// Report an error in C
// in C ++, the result a = 3

Trinocular operator

  • The three-mesh operator in the C language returns the value of the variable

    • cannot be used as a left value

  • The three-mesh operator in C + + can directly return the variable itself

    • Can be used as a right value or as an Lvalue

Attention:

If one of the values that the Trinocular operator may return is a constant value, it cannot be used as an lvalue

The trinocular operator can be used as an lvalue only if all of the possible returns are variables, and a constant variable cannot be used as an lvalue.


What is the significance of such an upgrade to the three-mesh operator in C + +?



When all the possible returns of the Trinocular operator are variables, the variable itself is returned , which leads to a new concept: reference



3. References



3.1 Variable Name



A variable is an alias for an actual contiguous storage space, in which a variable is used to request and name the storage space, and the storage space is available through the name of the variable.



Question: Can I have only one alias for a contiguous storage space?



3.2 References


References in C + +

  • Added the concept of references in C + +

    int a = 4;
    int & b = a; // b is an alias for a
    b = 5; // operation b is operation a
    
    • A reference can be considered an alias for a defined variable

    • Syntax for referencing:Type& name = var;

Attention:

A generic reference must be initialized with a variable of the same type when defined.

  • What does C + + do with the three-mesh operator?

    int a = 1;
    int b = 2;
    (a <b? a: b) = 3; // ok, returns a reference to a or b, which can be used as an lvalue
    (a <b? 1: b) = 4; // err, returns the value of 1 or b, cannot be used as an lvalue 
    • When the possible return of the trinocular operator is a variable, a variable reference is returned

    • When there are constants in the possible return of the trinocular operator, the value returned is


4. Summary


The
boolType is the new added base type for C + +

boolType values can be onlytrueandfalse

The three-mesh operator in

C + + can be used as an lvalue

A reference in

C + + can be used as an alias for a variable

Three the possible return of the mesh operator is a variable, the return is a reference

Related Article

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.