[StudyNote C++ Primer] Compound Types
Variables and Basic Types
Section 2.1 Primitive Built-in Types
2.1.1 Arithmetic Types
include two categories: Integral types(character and boolean types) and floating-point types
char | character | 8bits
wchar_t | wide character | 16bits
char16_t | Unicode character | 16bits
char32_t
int 16bits
long 32bits
float 6 significant digits
double 10 significant digits
wchar_t char16_t char32_t etc are used for extended character sets
the integral types may be signed or unsigned
A signed type represents negative or positive numbers (including 0)
and an unsigned type represents only values greater than or equal to 0
In an unsigned type, all the bits represent the value
and 8-bit unsigned char can hold the values from 0 through 255 inclusive
2.1.3 Literals
the value 20 in three ways:
20 --- decimal
024 -- octal (begain with 0)
0x14 - hexadecimal (begin with 0x or 0X)
'a' --- character literal
"Hello World!" --- string literal
Section 2.3 Compound Types
A compound type is a type that is defined in terms of another type.
two of which --- references and pointers
2.3.1 References
A reference defines an alternative name for an obj.
A reference type refers to another type
int T = 1024;
int &refT = t; // refT refers to (is another name for) T
we bind the reference to a variable's initializer, rather than its value
Once initialized, a reference remains bound to its inital object
int T = 1;
int &refT = T;
refT += 1;
std::cout << T << std::endl; // The value of T is two (for the refT is only another name of T)
2.3.2 Pointers
A pointer is a compound type that points to another types
Pointers can be assigned and copied;
a single pointer can point to several different objects over its lifetime
A pointer holds the address of another object
we get the address of an object by using the address-of operator (the $ & $ operator)
int T = 42;
int *p = &T;