A tour of C++ Book Note Day3
Just for share my note when I reading the book. If you want to discuss what interests you, you can write in the comments section. If there is a dispute, you can corret it!
1.4 Types Variables and Arithmetic
Types/type quote16
- 每一个变量名和表达式都有一个类型,而该类型决定了其可能被执行的操作
- 或者说,类型决定了该类型一系列可用的操作
Every name and every expression has a type that determines the operations that may be performed on it. ^quote16
Types/declaration ^quote17
- 声明是将一个实体引入进程序和指定其类型的语句
A declaration is a statement that introduces an entity into the program and specifies its type ^quote17
Types/value quote18
- 对象是包含(保存)某种类型值的内存空间
- 其实通俗点说就是实例化(对象)是一堆值的集合
- value是根据类型对一系列二进制集合的解释
- 也就是说,信息不仅仅只有表示,还有解释
- 带解释的信息被称为值 -> value, 而类型其实就是一种表示
An object is some memory that holds a value of some type.
A value is a set of bits interpreted according to a type. ^quote18
Variables/type quote19
- 类型的大小是由实现定义的。
- 因此,不同机器上对其的实现不同,其表现的类型大小也不尽相同
The size of a type is implementation-defined (i.e., it can vary among different machines)^quote19
Variables/tip quote20
- 为了使长字节文字更利于阅读,我们可以使用单引号(')左右一个数字分隔符
- 同时,0x的前缀表示十六进制数,0前缀表示八进制数,0b前缀表示二进制数
To make long literals more readable for humans, we can use a single quote (’) as a digit separator. ^quote20
double x = 3.1415'9265'4646;
double x1 = 0x7546'ddde'45;
double x2 = 04657'123;
double x3 = 0b0101'1100'1101;
1.4.1 Arithmetic
Arithmetic/trans1 quote21
- 因为历史优化的原因,其他表达式(例如
f
(
x
)
+
g
(
y
)
f(x)+g(y)
f(x)+g(y))和函数参数(例如
h
(
f
(
x
)
,
g
(
y
)
)
h(f(x), g(y))
h(f(x),g(y)))的运算顺序很不幸地是未定义的
这也就说明了,为什么谭教授的小红书害人不浅
For historical reasons realated to optimization, the order of evaluation of other expressions (e.g., f(x)+g(y)) and of function arguments (e.g.,h(f(x),g(y))) is unfortunately unspecified. ^quote21
// for exmaple, if we write a code
i = 5;
printf("%d%d%d", i++, ++i, i++);
// the code's output is unsoecified, maybe it was 8 8 8, or 5 5 8...
// in fact, it depends on the platform such as gcc, msvc or clang...
1.4.2 Initialization
Initialization/initializer_lists quote22
- =赋值操作符是传统的且来源于C的,但是如果你对值的类型不确定,使用常规的{}初始化列表可以避免这个问题,除此之外,它还能帮你避免隐式转换所导致的信息丢失
- 初始化列表规定了必须显示的初始化一个变量(对象) -> 保证了数据的一致性,且减少了隐式类型转换的发生
The = form is traditional and dates back to C, but if in doubt, use the general {}-list form. If nothing else, it saves you from conversions that lose information ^quote22
int i = 7.54; // in this statement, the i of value is 7, loss the float-point
int i {7.54}; // in this statement, it was error, can't pass the compile
Initialization/constant quote23
- 常量不能是未初始化的,且一个变量只有在极少数条件下才不会被初始化
- 对变量(甚至是常量)初始化是一个好习惯,尽管你可能当时没有使用
A constant cannot be left uninitialized and a variable should only be left uninitialized in extremely rare circumstances. ^quote23
Initialization/type quote24
- 对于内置类型(例如string、vector…)能够在定义的时候隐式的初始化
- 实际上就是在实例化时,C++默认调用了其默认构造函数
User-defined types (such as string, vector, Matrix, Motor_controller, and Orc_warrior) can be defined to be implicitly initialized ^quote24
Initialization/auto quote25
- 我们会在不需要在特殊情况下清晰的指明类型的时候使用auto,比如:
- 定义的类型范围很大,我们需要让阅读代码的人清晰的理解该类型
- 初始化类型不明确的时候
- 我们需要明确说明变量的范围或者精度的时候(例如,double 而非 float)
[!question] why when we need explicited variable to use auto rather that specific type?
because compiler is well to use that
We use auto where we don’t have a specific reason to mention the type explicitly. “Specific reasons” include:
The definition is in a large scope where we want to make the type clearly visible to readers of our code.
The type of the initializer isn’t obvious.
We want to be explicit about a variable’s range or precision (e.g., double rather than float). ^quote25
Initialization/auto_use quote26
- 这在范型编程中尤为重要,因为程序员很难知道一个对象的确切类型,而且这个对象的类型名可能很长
This is especially important in generic programming where the exact type of an object can be hard for the programmer to know and the type names can be quite long ^quote26