读书笔记:C++ primer 5th edition--chapter 6.函数

part1.参数传递
1.数组形参,可使用标准库规范
void print(const int *beg, const int *end){
     while(beg != end)
          cout << *beg ++ << endl;
}
usage:     int j[2] = {0, 1}
                  print(begin(j), end(j))
也可以显示传递表示数组大小的形参:void print(const int ia[], size_it size)

2.形参可以使数组的引用,此时形参绑定到对应的实参也就是数组上
//维度是类型的一部分,此时限制函数只能作用于大小为10的数组
void print (int (&arr)[10] ){
     for (auto elem : arr)
          cout << elem << endl;
}
3.多维数组:
void print(int (*matrix)[10], int rowSize) {};
可以看出,无论一维数组还是多维数组,因为传参时数组被退化为指针,因此数组的大小信息需要额外传递
4.可变形参:
如果类型一样,可以用initializer_list的标准库类型,它与vector一样是一种模板类型,但是值不可变
void error_msg(initializer_list<string> il){
     for(auto beg  = il.begin(); beg != il.end(); ++ beg
          cout << *beg << “ “;
     cout << endl;
}
usage: error_msg({“arg1”, expected, actual})
//why not use vector ?

part2.返回类型
1.可以返回引用,这里不会拷贝
const string & shorterString(const string &s1, const string &s1){
     return s1.size() < s2.size() ?s1 : s2;
}
2.但是不要返回局部对象的引用或者指针,因为已被销毁,未定义。
3.引用返回左值,此时函数调用放在赋值语句的左侧。。
char &get_val(string &str, string::size_type ix){
     return str[ix]
}
string s(“a value”);
get_val(s, 0) = ‘A’
4.c++11可以返回花括号包围的值的列表,相当于vector
vector <string> process(){
     ...
     return {“str1”, ”str2”};
}
5.main无法递归自己,也无法重载
6.返回数组指针
     int (* func ( int i ) )[10]
     func(int i)  表示调用func需要int类型的实参
     (* func(int i)) 表示可以对函数调用的结果 执行解引用
      (* func ( int i ) )[10] 表示解引用func的调用将得到一个大小为10的数组
     int (* func ( int i ) )[10] 表示数组元素类型是int
7.还可以使用尾置返回类型
     //与上述声明等价
     auto func(int i) -> int (*) [10]

part3.调试帮助
1.assert依赖于NDEBUG的预处理变量的状态。如果定义了NDEBUG,则assert什么都不做。
2.也可以使用NDEBUG编写调试代码。
void print(const int ia[], size_t size){
 #ifndef NDEBUG
     cerr << _ _ func_ _ << “: array size is “ << size << endl;
#endif
}
__func__     当前调试函数的名字
__FILE__     文件名字符串字面值
__LINE__     当前行号
__TIME__ 编译时间

part4.函数匹配与函数指针
1.确定可行函数
编译器选择那些形参数量满足要求且实参类型和形参类型可以匹配的函数。
2.从可行函数中选择与本次调用最匹配的函数,有且只有一个满足,则成功
1)该函数每个实参的匹配都不劣于其他可行函数需要的匹配
2)至少有一个实参的匹配优于其他可行函数提供的匹配
否则出现二义性。
3.函数指针
pf = lengthCompare;
pf = &lengthCompare;//二者等价
bool b1 = pf(“str1”,  “str2”);
bool b2 = (*pf) (“str1”,  “str2”);
bool b3 = lengthCompare(“str1”,  “str2”);//三者等价
4.函数指针形参
void userBigger(const string &s1, const string &s2, bool pf(const string &, const string &))
也可以直接把函数作为实参使用,此时它会自动转换成指针:
useBigger(s1, s2, lengthCompare);
为了避免冗长:
typedef bool (*FuncP) (const string &, const string &)
typedef decltype(lengthCompare) *FuncP2 //二者等价
用法变为:
     void useBigger(const string &, const string &, FuncP2)
5.返回指向函数的指针
int ( * f1 ( int ) ) int *, int )     //f1有形参列表,所以f1是函数,f1前面有*,所以返回指针。指针类型本身也包含形参列表,因此指针指向函数,该函数返回类型为int
等价于:
auto f1 (int ) -> int (*) (int*, int) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值