1 boolean type
-
Boolean types in C + +
- C + + adds bool to the basic type system of C language
- The only acceptable values of bool in C + + are true and false
- bool takes only one byte
-
be careful:
- True represents the true value, and the compiler uses 1 internally
- false represents a non true value, which is internally represented by 0 in the compiler
-
Boolean value
- bool type has only two values: true (non-0) and false (0)
- The C + + compiler converts non-0 values to true and 0 values to false
-
Examples of Boolean types
-
Demo
#include <stdio.h> int main(int argc, char *argv[]) { bool b = false; int a = b; printf("sizeof(b) = %d\n", sizeof(b)); //1 printf("b = %d, a = %d\n", b, a); //0,0 b = 3; //b = true; a = b; //a = 1 printf("b = %d, a = %d\n", b, a); //1,1 b = -5; //b = true a = b; //a = 1 printf("b = %d, a = %d\n", b, a); //1,1 a = 10; b = a; //b = false printf("a = %d, b = %d\n", a, b); //10,1 a = 0; b = a; //b = false printf("a = %d, b = %d\n", a, b); //0,0 return 0; }
-
-
Boolean type is the basic data type in C + +
- Global variables of type bool can be defined
- Constants of type bool can be defined
- A pointer of type bool can be defined
- An array of bool types can be defined
- . . .
2 ternary operator
-
C + + upgraded the ternary operator
-
Question: is the following code correct?
int a = 1; int b = 2; (a < b ? a : b) = 3; printf("a = %d,b = %d\n",a,b);
- C language compiler compilation: error: lvalue required as left operate of assignment
- C + + compiler compilation: correct: a = 3,b = 2
-
The ternary operator in C language returns a variable value and cannot be used as an lvalue
-
The ternary operator in C + + can directly return the variable itself, which can be used as both an R-value and an l-value
-
Note: if the ternary operator may return a constant value, it cannot be used as an lvalue
3 references
-
Variable name
- A variable is an alias for an actual contiguous storage space
- The program applies for and names the storage space through variables
- The storage space can be used by the name of the variable
-
Question: can only one alias be used for a continuous storage space?
- There can be multiple = > references
-
References in C + +
-
A reference can be seen as an alias for a defined variable
-
Syntax of reference: Type & name = VaR;
int a = 4; int& b = a; //b is the alias of a b = 5; //Operation b is operation a
-
Note: common references must be initialized with variables of the same type when they are defined
-
-
Reference example
-
Demo
#include <stdio.h> int main(int argc, char *argv[]) { int a = 4; int& b = a; b = 5; printf("a = %d\n", a); //5 printf("b = %d\n", b); //5 printf("&a = %p\n", &a); //&a = 0xbfe54aec printf("&b = %p\n", &b); //&b = 0xbfe54aec return 0; }
-
-
What does C + + do with ternary operators?
- When all the possible returns of the ternary operator are variables, the variable reference is returned
- When there is a constant in the possible return of the ternary operator, the value is returned
int a = 1; int b = 2; (a < b ? a : b) = 3; //Correct, return the reference of a or b, which can be used as an lvalue (a < b ? 1 : b) = 4; //Error, return the value of 1 or b, which cannot be used as an lvalue