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
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 + +
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