C++ Primer Plus第七天
复习函数的基本知识
为了提高效率,可以学习学习,STL和boost
前提:
提供函数原型:一般在头文件里写
提供函数定义
调用函数
函数参数的按值传递
void number (int a);
函数和数组
int sum(int a[],int size);
此时的数组参数不是副本,所以为了安全,加上const:
int sum(const int a[],int size);
补充:
int *p
const int *p:*p = 10 (NO) p= &a (OK)
int * const p
const int * const p;
函数和二维数组
int sum(int a[][4],int size);
补充:
a[b][c] = *(*(a+b)+c);
=*(a+b)[c];
函数和C风格字符串
因为字符串有固定的结束符,所以可以不用加size;
int chars(char *p)
函数和结构
可以传值,可以传地址;
函数和string对象
多个字符串
void mypoint(string a [],int a)
函数与array对象
std::array<double,4>expenses;
按值:show(std::array<double,4> expenses);
地址:show(std::array<double,4> *expenses);
递归
调用自己~必须要给结束方式
函数指针
int (*p) (int a,int b);//函数指针
int (*p[3])(int ,int);
int * p (int a,int b);//指针函数
int (*p)(int,int) = fun1;
利用typedef 起别名:
typedef int (*p)(int,int);
p n1 = fun1;
p n2[3];
n2[0] = fun2;
n2[1] = fun3;
n2[2] = fun4;
补充:auto 可以让编译器自己辨别类型