2017.2.18

1.函数包装器

1.设计通用的函数执行接口,可以设置计数(函数执行次数)和关卡
2.函数包装器依赖于函数模板,实现通用泛型

3.函数代码可以内嵌在函数中

最基本模板:

  1 //函数包装器, T数据类型, F是函数
  2 template<typename T,typename F>
  3 T run(T v, F f)
  4 {
  5 	static int count = 0;
  6 	count++;//计数器
  7 	std::cout << "run  一个参数的包装器 执行" << count << "" << std::endl;
  8 	if (count > 1)
  9 	{
 10 		T  vx(0);
 11 		return  vx;
 12 	}
 13 	return f(v);//函数传入参数
 14 }
 15 int  cheng(int a, int b)
 16 {
 17 	return a*b;
 18 }
 19 
 20 int  main()
 21 {
 22         function<double(double)> fun1 = [](double u)
 23 	{
 24 		return u * 2;
 25 	};
 26 	function<double(double)> fun2 = [](double u)
 27 	{
 28 		return u*u;
 29 	};
 30 	function<int(int, int)> fun3 = [](int u1,int u2)
 31 	{
 32 		return u1 + u2;
 33 	};
 34 	function<int(int, int)> fun4 = cheng; //fun4函数指针
 35 }

2.函数模板的覆盖

 

 

3.类型转换

static_cast<需要转换的数据类型>(要转换的数据)       相当于c中的强制转换,可以用于基础类型,在编译期进行

const_cast<需要转换的数据类型>(要转换的数据)           去const,但是const并没有被改变

reinterpret_cast<需要转换的数据类型>(要转换的数据)  

dynamic_cast_cast<需要转换的数据类型>(要转换的数据)  只接受基于类对象的指针和引用的类型转换

4.

posted @ 2017-02-18 17:12  acliang  阅读(151)  评论(0编辑  收藏  举报