C++ Primer第五版 第六章 函数

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 }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值