练习2.1
c++保证short和int至少是16位的,long至少是32位,longlong至少是32位;
有符号类型可以表示正数,负数和零,而无符号类型只能表示不小心于零的数;
float是单精度浮点数,double是双精度浮点数。
练习2.2
使用double或者float
练习2.3
32 4294967264 32 -32 0 0
Exercise 2.10
What are the initial values, if any, of each of the following variables?
std::string global_str; int global_int; int main() { int local_int; std::string local_str; }
global_str
is global variable, so the value is empty string.global_int
is global variable, so the value is zero.local_int
is a local variable which is not uninitialized, so it has a undefined value.local_str
is also a local variable which is not uninitialized, but it has a value that is defined by the class. So it is empty string.
Exercise 2.11
Explain whether each of the following is a declaration or a definition:
- (a)
extern int ix = 1024;
- (b)
int iy;
- (c)
extern int iz;
- (a): definition.
- (b): definition.
- (c): declaration.
练习2.19
说明指针和引用之间的关键区别。
定义:
指针是“指向”任何其他类型。
引用是对象的 “另一个名称” 。
关键区别:
- 引用是已存在对象的另一个名称。指针就是它自己的对象。
- 一旦初始化,引用仍然绑定到其初始对象。有没有办法重新绑定的引用来引用不同的对象。可以分配和复制指针。
- 引用总是获取引用最初绑定到的对象。一个指针可以在其一生中指向几个不同的对象。
- 引用必须被初始化。一个指针在定义时不需要被初始化