chapter6.cpp:
#include "stdafx.h"
#include "Chapter6.h"
#include <string>
#include <iostream>
using namespace std;
typedef int Func(int, int);
//该函数返回两个数中最大的那个
int GetMax(int a, int* b)
{
return a > *b ? a : *b;
}
void print(int *p)
{
cout << *p << endl;
}
//引用打印数组
void printArr(int (&arr)[2],size_t len)
{
size_t i = 0;
while (i<len)
{
cout << *(arr + i) << endl;
i++;
}
}
void printTest(const int ia[10])
{
for (size_t i = 0; i != 10; i++)
{
cout << ia[i] << ",";
}
}
//求list中的阶加
int lstCount(initializer_list<int> list)
{
int cnt = 0;
for (const auto var : list)
{
cnt += var;
}
return cnt;
}
char& get_val(string &str, string::size_type i)//如果形参不是引用,则带入函数的是实参的一个副本
{
return str[i];//返回值是str[i]的引用
}
void setDefaultParam(int dft)
{
cout << dft << endl;
}
//下面两个函数用于演示函数指针
int get_sum(int a, int b)
{
return a + b;
}
int get_diff(int a, int b)
{
return a - b;
}
int get_mul(int a, int b)
{
return a*b;
}
int get_div(int a, int b)
{
return a / b;
}
//该函数的参数是一个函数指针,声明形式和普通的函数指针声明方式一样
int get_func_pointer(Func p ,int a,int b)
{
return p(a,b);
}
chapter6.h:
//头文件
#include <string>
using namespace std;
typedef int Func(int, int);//Func为int(int,int)的函数类型
//typedef decltype(get_sum) Func2;
//Func和Func2声明了函数的类型,即get_sum/get_diff的函数类型
//简单来说,就是使用typedef来定义自己的类型(在此处表示为Func,即类型为int(int,int))
int GetMax(int a, int* b);
void print(int *p);
void printArr(int(&arr)[2], size_t len);
void printTest(const int ia[10]);
int lstCount(std::initializer_list<int> list);
char& get_val(string &str, string::size_type i);
//函数的默认实参声明在函数的声明中,即头文件中
//一般来讲一个函数只声明一次,但多次声明也是合法的
//但要注意的是一个形参只能被赋予一次默认值
void setDefaultParam(int dft = 1);
//void setDefaultParam(int = 1);//也可以使用int=1这样的形式
int get_sum(int a, int b);
int get_diff(int a, int b);
int get_mul(int a, int b);
int get_div(int a, int b);
int get_func_pointer(Func, int a, int b);
main:
// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>//<>直接从编译器自带的函数库中寻找文件
#include <string>//使用字符串需要包含<string>
#include <vector>
#include "SalesItem.h"//""先从自定义的文件中找 ,如果找不到在从函数库中寻找文件
#include "Chapter6.h"//将函数写在头文件中,并包含进来
//命名空间:
using namespace std;
using std::vector;
//函数声明:
void func(int);
int GetMax(int a, int* b);
//size_t count_calls();
int main(int argc, char *argv[])
{
//---------------------6.2.4 节练习---------------------
//练习6.21
/*
int a = 10;
int b = 20;
int c = GetMax(a, &b);
cout << c << endl;
*/
//练习6.23
/*
int i = 10;
print(&i);//指针,传引用
int arr[2] = { 1,2 };
printArr(arr,2);
*/
//练习6.24
/*
int arr2[10] = { 1,2,3,4,5,6,7,8,9,10 };
printTest(arr2);
*/
//---------------------6.2.6 含有可变形参的函数---------------------
//为了能编写实参数量可变的函数c+11提供了两种解决办法,其中一种是传递一个名为initializer_list的标准库类型
//类似java中的List<T>(?)
//但该方法的前提是实参类型一致
//initializer_list类型用于表示某种特定类型的值数组,它定义在initializer_list.h中,它也是一种模板类型
//这是c++11中的新特性
//与vector的区别在于,initializer_list中的元素永远是常量,无法改变
//可以用foreach来遍历其中的元素
//---------------------6.2.6 节练习---------------------
//练习 6.27
/*
initializer_list<int> lst { 1,2,3,4,5,6,7,8,9,10 };
initializer_list<int> list2(lst);//两个list共享元素
cout << lstCount(lst) << endl;
*/
//当一个函数的返回类型是引用时,则函数调用可以是左值
/*
string str("hello");
get_val(str,0) = 'H';//返回str[0]的引用
cout << str << endl;
*/
//-------------------- - 6.3.3 返回数组指针---------------------
//要返回一个数组的指针,方法之一是使用类型别名,即typedef关键字
//typedef int arrT[10];//arrT表示一个10个正形数组的类型
//using arrT = int[10];//等价
//如果不想使用类型别名,则使用如下方法:
//int(*func(int i))[10];
//func(int i)表示调用func需要一个int参数
//(*func(int i))表示对函数的调用结果进行解引用操作
//int (*func(int i))[10]解引用的结果是一个大小为10的数组,其元素类型为int
//还可以通过尾置返回类型来简化上述操作:
//auto func(int i)->int(*)[10];
//尾置返回类型是C++11的新特性,一般用于复杂的函数和返回类型(如指针)
//尾置返回类型在形参列表后面跟一个->,前面的尾置用auto
//以下是一个使用函数默认参数的例子
/*
int temp = 2;
setDefaultParam();//使用函数的默认实参
setDefaultParam(temp);//不使用默认实参
*/
//--------------------- 6.7 函数指针---------------------
//即指针指向的是一个函数,指针指向特定的类型,该类型为函数的返回类型和形参决定
//可以通过C#中的delegate来理解函数指针
//
bool lenCompare(const string&, const string&);//该函数的类型是bool(const string&,const string&)
//要声明一个指向该函数的指针,只要把函数名换成指针即可
//bool (*p)(const string&, const string&);//一个函数指针,如果不加括号,则p是一个返回值为 bool*的函数
//以下是一个使用函数指针的例子/
/*
int (*pf)(int a, int b);//首先声明一个指向函数的指针
//int(*pf)(int, int);
pf = get_sum;//使其指向get_sum函数
cout << pf(1,2) << endl;//1+2=3
pf = get_diff;//指向其他函数
cout << pf(5, 1) << endl;//5-1=4
pf = get_sum;//pf重新指向get_sum
cout << "将函数指针作为参数:" << get_func_pointer(pf,10,10) << endl;//将函数指针作为参数,10+10=20
cout << "我们还可以直接把函数作为参数,此时它会自动转换为指针:" << get_func_pointer(get_diff, 10, 5) << endl;//10-5=5
//直接使用函数指针类型会让代码显得冗长繁琐,解决办法之一是:使用decltype和typedef关键字
//同样的形式展现在Chapter6.cpp和Chapter6.h中(typedef和decltype)
typedef int(*fp)(int, int);//fp指向int(int,int)类型的函数
typedef decltype(get_sum) *fp2;//等价,fp2指向get_sum函数类型的函数
cout << "返回5x5的结果:" << get_func_pointer(get_mul, 5, 5) << endl;
pf = get_mul;
cout << "等价:" << get_func_pointer(pf, 5, 5) << endl;
*/
//--------------------- 6.7 节练习---------------------
//练习6.54
vector<int (*)(int, int)> pvec;//vector中保存的是指向函数的指针
pvec.push_back(get_sum);
pvec.push_back(get_diff);
pvec.push_back(get_mul);
pvec.push_back(get_div);
int len = pvec.size();
for (int i=0; i<len; i++)
{
cout<< get_func_pointer(pvec[i],10,5) << endl;
}
system("pause");
return 0;
}
void func(int n)
{
cout << n << endl;
}