Cpp摘记:函数指针、函数模版、类模板
- 1. 函数指针
- 2. 函数模版
- 3. 类模板
1. 函数指针
\qquad
函数指针是一个很有意思的功能,在阅读代码的时候深有感触。一般所说的指针是指向变量,可以用指针指向不同变量的地址;函数指针也类似,函数指针可以指向不同函数的地址(函数名就是函数的地址)。
\qquad
例:
double func(double);
\qquad\text{double func(double); }
double func(double); 如果已定义一个函数 double func(double)
double (*pf)(double);
\qquad\text{double (*pf)(double);}
double (*pf)(double); 可以定义一个具有相同形参和返回值类型的函数指针 *pf
pf = func;
\qquad\text{pf = func;}
pf = func; 可以用函数指针 pf 指向函数 func
,使用时可以用 *pf
代替 func
a1 = func(3);
\qquad\text{a1 = func(3); }
a1 = func(3); 通过函数名调用 func()
函数
a2 = (*pf)(3);
\qquad\text{a2 = (*pf)(3);}
a2 = (*pf)(3); 通过函数指针 *pf
调用 func()
函数,把 (*pf)
当成函数名
或
a2 = pf(3);
\quad\text{a2 = pf(3); }
a2 = pf(3); 也可以像使用函数名那样使用函数指针 *pf
写成
(*pf)
是因为小括号优先级更高,否则 double *pf(double) \text{double *pf(double)} double *pf(double) 就是指“返回值为double指针”的函数
\qquad
在下例中,定义了一个表示运算的函数指针
*calc
\text{*calc}
*calc,可以指向不同函数。
(
1
)
\qquad(1)
(1) 当
*calc
\text{*calc}
*calc 指向“加法运算”函数
addition
\text{addition}
addition 时,
(*calc)(x,y)
\text{(*calc)(x,y)}
(*calc)(x,y) 执行的是
x
+
y
x+y
x+y 的加法操作;
(
2
)
\qquad(2)
(2) 当
*calc
\text{*calc}
*calc 指向“乘法运算”函数
multiplication
\text{multiplication}
multiplication 时,
(*calc)(x,y)
\text{(*calc)(x,y)}
(*calc)(x,y) 执行的是
x
×
y
x\times y
x×y 的乘法操作;
(
3
)
\qquad(3)
(3) 函数指针也可以作为函数的形参。
#include <iostream>
using namespace std;
double addition(double, double);
double multiplication(double x, double y);
void calculation(double (*pf)(double, double), double x, double y);
int main()
{
// 定义函数指针
double (*calc)(double, double);
// 函数指针指向“加法运算”
calc = addition;
double result = (*calc)(5,6);;
cout << result << endl;
result = calc(7,8);
cout << result << endl;
// 函数指针指向“乘法运算”
calc = multiplication;
result = (*calc)(5,6);
cout << result << endl;
result = calc(7,8);
cout << result << endl;
// 将“函数指针”作为函数calculation的形参
calculation(addition,8,9);
calculation(multiplication,8,9);
return 0;
}
double addition(double x, double y)
{
cout << "The sum of " << x << " and " << y << " is ";
return x + y;
}
double multiplication(double x, double y)
{
cout << "The product of " << x << " and " << y << " is ";
return x * y;
}
void calculation(double (*pf)(double, double), double x, double y)
{
cout << pf(x,y) << endl;
}
运行结果:
The sum of 5 and 6 is 11
The sum of 7 and 8 is 15
The product of 5 and 6 is 30
The product of 7 and 8 is 56
The sum of 8 and 9 is 17
The product of 8 and 9 is 72
2. 函数模版
\qquad 函数模板是函数的泛型描述,使得函数能够自动适配数据类型。
\qquad
函数模板的声明方式(使用
typename
\text{typename}
typename关键字表示数据类型):
template <
typename
Any> void func(Any &, Any &);
\qquad\qquad\text{template <\textcolor{crimson}{\text{typename}} Any> void func(Any \&, Any \&);}
template <typename Any> void func(Any &, Any &);
#include <iostream>
using namespace std;
template <typename Any> void Swap(Any &, Any &);
template <typename Any> void Swap(Any *, Any *, int); // 函数模板也可以重载
template <typename Any> void arr_show(Any *, int);
int main()
{
int i = 10;
int j = 20;
cout << "i = " << i << ", j = " << j << ".\n";
Swap(i,j);
cout << "swap i,j: \n";
cout << "i = " << i << ", j = " << j << ".\n\n";
double x = 21.2;
double y = 36.7;
cout << "x = " << x << ", y = " << y << ".\n";
Swap(x,y);
cout << "swap x,y: \n";
cout << "x = " << x << ", y = " << y << ".\n\n";
double arr1[] = {2,4,6,8,10,12,14,16};
double arr2[] = {1,3,5,7,9,11,13,15};
arr_show(arr1,8);
arr_show(arr2,8);
Swap(arr1,arr2,4);
cout << "Swap ......" << endl;
arr_show(arr1,8);
arr_show(arr2,8);
return 0;
}
template <typename Any> void arr_show(Any *arr, int n)
{
for(int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
template <typename Any> void Swap(Any &a, Any &b)
{
Any temp;
temp = a;
a = b;
b = temp;
}
template <typename Any> void Swap(Any *a, Any *b, int n)
{
Any temp;
for(int i = 0; i < n; i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
运行结果:
i = 10, j = 20.
swap i,j:
i = 20, j = 10.
x = 21.2, y = 36.7.
swap x,y:
x = 36.7, y = 21.2.
2 4 6 8 10 12 14 16
1 3 5 7 9 11 13 15
Swap ......
1 3 5 7 10 12 14 16
2 4 6 8 9 11 13 15
3. 类模板
\qquad
类模板的声明方式(使用
Type
\text{Type}
Type关键字表示数据类型):
template <class
Type
>
\qquad\qquad\text{template <class \textcolor{crimson}{\text{Type}}>}
template <class Type>
class myClass
\qquad\qquad\text{class myClass}
class myClass
\qquad
成员函数的声明方式:
Type
func(
Type
&x,
Type
&y);
\qquad\qquad\text{\textcolor{slateblue}{\text{Type}} func(\textcolor{blue}{\text{Type}} \&x, \textcolor{blue}{\text{Type}} \&y);}
Type func(Type &x, Type &y);
\qquad
成员函数的实现方式:
template <class
Type
>
\qquad\qquad\text{template <class \textcolor{crimson}{\text{Type}}>}
template <class Type>
Type
myClass<
Type
>::func(
Type
&x,
Type
&y);
\qquad\qquad\text{\textcolor{slateblue}{\text{Type}} myClass<\textcolor{crimson}{\text{Type}}>::func(\textcolor{blue}{\text{Type}} \&x, \textcolor{blue}{\text{Type}} \&y);}
Type myClass<Type>::func(Type &x, Type &y);
#include <iostream>
using namespace std;
template <class Type>
class myCalc
{
private:
public:
Type addition(Type &x, Type &y);
Type multiplication(Type &x, Type &y);
};
template <class Type>
Type myCalc<Type>::addition(Type &x, Type &y)
{
cout << "The sum of " << x << " and " << y << " is ";
return x + y;
}
template <class Type>
Type myCalc<Type>::multiplication(Type &x, Type &y)
{
cout << "The product of " << x << " and " << y << " is ";
return x * y;
}
int main()
{
myCalc<int> cal1;//定义一个int型计算器
int x1 = 6;
int y1 = 7;
cout << cal1.addition(x1,y1) << endl;
cout << cal1.multiplication(x1,y1) << endl;
myCalc<double> cal2;//定义一个double型计算器
double x2 = 2.5;
double y2 = 3.7;
cout << cal2.addition(x2,y2) << endl;
cout << cal2.multiplication(x2,y2) << endl;
return 0;
}
运行结果:
The sum of 6 and 7 is 13
The product of 6 and 7 is 42
The sum of 2.5 and 3.7 is 6.2
The product of 2.5 and 3.7 is 9.25