2 Variables and Basic Types:
int
long
bool
float 3.21
double 5.4321
sign/ unsign
cast
int: -2^31 - 2^31 - 1
long: -2^53 - 2^63 - 1
Type conversion
literal: Literals are data used for representing fixed values. Ex:
string = "Hello World"
int = 1
long a = 31L;
extern int i = 1;
**** It is an error to provide an initializer on an extern inside a function.
example:
// bank account 1 ~ 9999 9999
declaration & definition
int bank_account_number = 1;
// in different cpp file, we can write:
extern int bank_account number;
-c 直接编译然后和别的文件link
-o 直接生成二进制文件
写一个脚本 run.sh
make clean
make
2.1 Primitive built-in Type
arithmetic types:
1, integer types: bool and character
2, floating types
special type named void
If we assign an out-of-range value to an object of signed type,
the result is undefined.
signed char c2 = 256; // assuming 8-bit chars, the value of c2 is
undefined
unsigned u = 10;
signed i = -1;
// convert signed to unsigned if they operate together!
std::cout << u + i << std::endl;
expected: 4294967305 (2^32 + 10)
Caution: Don’t Mix Signed and Unsigned Types
google c++ style guide
int a_b;
// scope: 尽量不要把local和全局变量同名
Literals:
decimal: 20
octal: 020
hexadecimal: 0x20
The compiler appends a null character (’\0’) to every string literal
2.2 variables:
Initialization is not assignment. Initialization happens when a variable is given a value when it is created. Assignment obliterates an object’s current value
and replaces that value with a new one
int i = 0;
int i = {0};
int i{0};
int i(0);
Caution: Uninitialized Variables Cause Run-Time Problems
difference between declaration & definition:
Identifiers:
int a, a_b, aB, AB; // declaration
scope of a name:
the scope resolution operator :: is used to access a global variable
or function from within a local scope
Example:
#include <iostream>
using namespace std;
int i = 10; // global variable
int main() {
int i = 5; // local variable
cout << "Local i = " << i << endl; // prints "Local i = 5"
cout << "Global i = " << ::i << endl; // prints "Global i = 10"
return 0;
}
2.3 Compound types:
- reference: (一个变量的多个同义名)
-
bind to a local variable
-
cannot bind literal / const
-
int& a = 12; // error: non-const lvalue cannot bind to a temporary of int
-
编译器认为你尝试去改变一个literal 12,而不是一个temporary 如果是const int& a = 12; 就可行,因为编译器会实际这么操作: const int temp = 12; // 编译器帮助我们生成一个temp a = temp;
-
int b; int& a = b; // a do not have a real address, ot has the same address to b
-
string a ="hi"; string& b = "hi"; // "hi" is a const char * type, 没有固定内存地址 // non const lvalue cannot bind to a // value of unrelated type const char *
-
const int& a = 12; // correct
-
- pointer :
- int a = 10;
- int *pa = &a;
- cout << &a;
- cout << pa;
- cout << *pa;
nullptr:
int *p1 = nullptr; // equivalent to int *p1 = 0;
int *p2 = 0; // directly initializes p2 from the literal constant 0
int *p3 = NULL; // equivalent to int *p3 = 0;
nullptr can be converted to another other pointer type!
void* pointer
The type void* is a special pointer type that can hold the address of any object!
Exercise 2.18: Write code to change the value of a pointer. Write code to
change the value to which the pointer point
int i = 0, j = 10;
int *pi = &i;
pi = j; // pi 指向j (pi中原来存储i的地址,现在存储j的地址)
*pi = 20; // j = 20;
Exercise 2.19: Explain the key differences between pointers and references.
same: both are declared to access an object!
*****
ref VS Pointer:
1, reference must initialize!
Pointers can be reassigned to point to different objects, while
references cannot be reassigned once they are initialized.
2, reference is not object, pointer is object.
Exercise 2.23: Given a pointer p, can you determine whether p points to a
valid object? If so, how? If not, why not?
No, pointer is just a variable taht stores the address of an object.
Exercise 2.24: Why is the initialization of p legal but that of lp illegal?
Because void pointer can hold address of any object, but long pointer can only
hold address of long type.
Key Concept: Some Symbols Have Multiple Meanings: operator and decalration
&
*
Compound Type Declarations:
Defining Multiple Variables:
int* p; // legal but might be misleading
int* p1, p2; // p1 is a pointer to int; p2 is an int
int *p1, *p2; // both p1 and p2 are pointers to int
The easiest way to understand the type of r is to read the definition right to left.
Example:
int i = 42;
int *p; // p is a pointer to int
int *&r = p; // r is a reference to the pointer p
r = &i; // r refers to a pointer; assigning &i to r makes p point to i
*r = 0; // dereferencing r yields i, the object to which p points; changes i to 0
-------****************************-------
int *&r = p;
// First, & means r is a reference!
// Next, * means &r is a pointer to object p
// So, r is a reference pointer to the object p
-------****************************-------
Exercise 2.25: Determine the types and values of each of the following
variables.
(a) int* ip, &r = ip;
ip: pointer to int;
r: reference to point ip;
2.4 Const Qualifier
const value must initialize!
const int i = 1;
non const -> const
int i = 1;
const int pi = i;
const !-> non-const
const int i = 123;
int &ri = i; // error
ri = 456;
const int i = 123;
// p is a pointer, pointing to const int
const int *p = i;
-------****************************-------
const pointers:
int errNumb = 0;
int *const curErr = &errNumb; // curErr will always point to errNumb
const double pi = 3.14159;
const double *const pip = π // pip is a const pointer to a const
object
read from right to left.
1, const curErr: curErr is a const object;
2, *: curErr is a const pointer;
3, base type is int: const pointer to an object of type int;
similarly: pip is a const pointer to an object of type const double;
-------****************************-------
Top level const
top level const: the pointer it self is a const
// top level -> object
const int *const pvalue = &i;
low level const: the pointer point to a const object
// low level -> base type (int ...)
const int *pvalue = &i;
Exercise 2.27: Which of the following initializations are legal? Explain why.
(a) int i = -1, &r = 0;
(b) int *const p2 = &i2;
(c) const int i = -1, &r = 0;
(d) const int *const p3 = &i2;
(e) const int *p1 = &i2;
(f) const int &const r2;
(g) const int i2 = i, &r = i;
(a) int i = -1, &r = 0; is illegal. The reference r must refer to an object,
but 0 is not an object. It is a literal value.
(c) const int i = -1, &r = 0; is legal. The reference r can refer to a temporary
object (in this case, the literal value 0) as long as it is of the same type as
the reference.
(e) const int *p1 = &i2; is legal. This declares a pointer to a constant integer,
which is initialized to the address of i2.
Types Alias:
using
auto
int i = 1;
decltype(i) j =456; // int j = 456