6.1
实参是形参的初始值,形参在函数调用完后就被释放了,两者的生命周期不同。
6.8
1 #ifndef CHAPTER6_H
2 #define CHAPTER6_H
3 int fact(int );
4
5
6 #endif
6.9
并排打开三个文件:vim -o 6.8.h 6.8.cpp 6.8_1.cpp
6.16
1.局限性:我们不能把const对象、字符串或者需要类型转换的对象传递给普通的引用形参;
2.尽量使用常量引用
6.17
不一样,其中一个会改变所引用对象的值,所以不能用常量引用。
1 #include <iostream>
2 #include <string>
3 #include <cctype>
4 using namespace std;
5
6 bool In_upper (const string &s)
7 {
8 for (auto &c : s)
9 if (isupper(c))
10 return true;
11 return false;
12 }
13 void To_upper (string &s)
14 {
15 for (auto &c : s)
16 c = toupper(c);
17 }
18 int main()
19 {
20 string s("zhangHaiwen");
21
22 cout <<boolalpha << In_upper(s) << endl;
23 To_upper(s);
24 cout << s << endl;
25
26 }
6.20
意外地修改了引用对象的值。
6.22
1 #include <iostream>
2 using namespace std;
3 void exchange (int *&p1, int *&p2)
4 {
5
6 cout << *p1 << ',' << *p2 << endl;
7 cout << p1 << ',' << p2 << endl;
8 swap(p1, p2);
9 cout << *p1 << ',' << *p2 << endl;
10 cout << p1 << ',' << p2 << endl;
11 }
12
13 int main()
14 {
15 int i = 1, j = 2;
16 int *p1 = &i, *p2 = &j;
17
18 cout << "*p1 = " << *p1 << ", *p2 = " << *p2 << endl;
19 cout << p1 << ',' << p2 << endl;
20 exchange(p1, p2);
21 cout << "*p1 = " << *p1 << ", *p2 = " << *p2 << endl;
22 cout << p1 << ',' << p2 << endl;
23 }
6.26
1 #include <iostream>
2 using namespace std;
3
4 int main(int argc, char *argv[])
5 {
6 cout << argc << endl;
7 auto p = &argv;
8 while (*(++(*p)))
9 cout << **p << ',';
10 cout << endl;
11 }