File tree 4 files changed +32
-3
lines changed
4 files changed +32
-3
lines changed Original file line number Diff line number Diff line change 4
4
5
5
## 基础概念和必要语法复习 <a href =" #basic " id =" basic " ></a >
6
6
7
- * [ side-effect 副作用] ( basic/ sideeffect-fu-zuo-yong.md)
8
- * [ lvalue vs rvalue 左值与右值] ( basic/ lvalue-vs-rvalue-zuo-zhi-yu-you-zhi.md)
7
+ * [ side-effect 副作用] ( sideeffect-fu-zuo-yong.md )
8
+ * [ lvalue vs rvalue 左值与右值] ( lvalue-vs-rvalue-zuo-zhi-yu-you-zhi.md )
9
9
* [ const 常量] ( basic/const-chang-liang.md )
Original file line number Diff line number Diff line change 1
1
# const 常量
2
2
3
+ 在C++中,常量一般有四种位置,为了叙述方便我临时将他们加上编号:
4
+
5
+ ```
6
+ int const0 a=0;//与 const0 int a=0 等价
7
+
8
+ int const1 foo(int const2 bar) const3{
9
+
10
+ }
11
+ ```
12
+
13
+ const0 意味着对左值` a ` 的声明为一个常量,该常量的本质是只能绑定一次的定位符
14
+
15
+ const1意味着返回值是一个常量,这种写法在C11后逐渐变得不再推荐(这个说法是在英文互联网中搜到的),比如:
16
+
17
+ ```
18
+ const int foo() {
19
+ return 3;
20
+ }
21
+ int main() {
22
+ int x = foo(); // copies happily
23
+ x = 4;// copies happily
24
+ }
25
+ ```
26
+
27
+ 可以正常编译并运行
28
+
29
+ 如果你觉得const1的这种情况不好理解的话,请看上一小节最后部分中,[ Mohan] ( https://stackoverflow.com/a/38559710/1433373 ) 提到的例子
30
+
31
+ {% embed url="https://stackoverflow.com/questions/3247285/const-int-int-const " %}
Original file line number Diff line number Diff line change @@ -175,7 +175,7 @@ b = std::move(tmp);
175
175
176
176
下面的图可能会让你更好理解他们的关系,以及为什么我在前面花费了大量篇幅讲解**表达式和左值的关系**
177
177
178
- ********
178
+ ********
179
179
180
180
另外,为了让你更深刻理解,这里节选翻译一段[Mohan](https://stackoverflow.com/a/38559710/1433373) 的备忘录,他是C++早期标准的起草人之一
181
181
File renamed without changes.
You can’t perform that action at this time.
0 commit comments