第八章 函数探幽
inline
内联函数
内联函数是C++为提高程序运行速度所做的一项改进。
内联函数的一般写法是,省略函数原型,将整个定义放在函数原型的位置,并在函数返回值类型前加上inline
。
内联函数一般适用于调用频繁但函数体简单的函数。
引用变量
引用变量的主要作用是用作函数的形参,使得函数使用原始数据而不是副本,达到和指针类似的效果。
创建引用变量:
int rats;
int &rodents = rats;
必须在声明时初始化引用变量。
引用变量和被引用变量指向同一个内存地址,因此,引用变量可以理解为一个变量有两个标识符,即用其中一个标识符改变变量的值,另一个标识符表示的变量的值也会相应改变。
语句int &rodents = rats;
和int * const pr = &rats;
类似。
#include <iostream>
using namespace std;
int main()
{
int rats = 4;
int& rodents = rats;
printf("rats = %d, rodents = %d.\n", rats, rodents);
rats++;
printf("rats = %d, rodents = %d.\n", rats, rodents);
rodents *= 4;
printf("rats = %d, rodents = %d.\n", rats, rodents);
return 0;
}
运行结果:
rats = 4, rodents = 4.
rats = 5, rodents = 5.
rats = 20, rodents = 20.
按引用传递
引用被用作函数参数,使得函数中的变量名成为调用程序中的变量的别名。这和指针的作用类似:
#include <iostream>
using namespace std;
void swap_usual(int a, int b);
void swap_reference(int &a, int &b);
void swap_pointer(int* a, int* b);
int main()
{
int a = 4, b = 2;
swap_pointer(&a, &b);
printf("a = %d, b = %d.\n", a, b);
swap_reference(a, b);
printf("a = %d, b = %d.\n", a, b);
swap_usual(a, b);
printf("a = %d, b = %d.\n", a, b);
return 0;
}
void swap_usual(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void swap_reference(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void swap_pointer(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
运行结果:
a = 2, b = 4.
a = 4, b = 2.
a = 4, b = 2.
如果不希望函数修改传入的引用,可以使用const
。
void show(const int& a);
函数重载
函数特征标
对于一个函数,函数特征标是指参数的数目和类型。
如果两个函数的参数数目和类型相同,同时参数的排列顺序也是相同的,则他们的特征标是相同的。函数的返回值类型和形参变量名于特征标无关。
函数重载
函数重载能够将多个函数使用同一个函数名,前提是重名的函数的特征标是不同的。在使用函数是,C++根据传递的参数自动匹配特征标相同的函数进行调用。
函数重载适用于函数基本上执行相同的任务,但使用不同类型的数据时。
#include <iostream>
using namespace std;
void print(string);
void print(int[], int);
void print(long long);
void print(int);
void print(string[], int);
int main()
{
string A{ "Hallo world." };
print(A);
int arr[3] = { 2, 4, 6 };
print(arr, 3);
long long x = 12423234233;
print(x);
int a = 3;
print(a);
string arrstr[3] = { "a", "vv", "ds" };
print(arrstr, 3);
return 0;
}
void print(string a)
{
cout << a << endl;
}
void print(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << endl;
}
void print(long long a)
{
printf("A long long number is ");
cout << a << endl;
}
void print(int a )
{
cout << "You put a " << a << endl;
}
void print(string[], int)
{
cout << "You put too much string!!\n";
}
运行结果:
Hallo world.
2
4
6
A long long number is 12423234233
You put a 3
You put too much string!!
函数模板
函数模板时通用的函数描述,它使用一个泛型编写函数,在调用函数时,程序根据参数的类型将泛型自动置换为合适的类型。
例如如果希望编写一个交换两个int
变量的函数,再编写一个交换两个long long
变量的函数,则可以使用一个函数模板,而不用写两个语句相同,仅仅变量类型不同的函数。
交换两个变量的函数模板:
template<typename Anytype>
void SWAP(Anytype M, Anytype N);
...
template<typename Anytype>
void SWAP(Anytype M, Anytype N)
{
Anytype temp = M;
M = N;
N = temp;
}
其中,typename
可以换为class
。
示例:
#include <iostream>
using namespace std;
template<typename T>
T Plus(T a, T b);
template<typename T>
void Swap(T& a, T& b);
int main()
{
string A = "Nice to meet you.", B = "Hallo world.";
cout << "A = " << A << endl;
cout << "B = " << B << endl;
Swap(A, B);
printf("Changed.\n");
cout << "A = " << A << endl;
cout << "B = " << B << endl;
//
double a = 1.39, b = 2.83;
cout << Plus(a, b) << endl;
int x = 3, y = 32;
cout << Plus(x, y) << endl;
return 0;
}
template<typename T>
T Plus(T a, T b)
{
T temp = a + b;
return temp;
}
template<typename T>
void Swap(T& a, T& b)
{
T temp = a;
a = b;
b = temp;
}
运行结果:
A = Nice to meet you.
B = Hallo world.
Changed.
A = Hallo world.
B = Nice to meet you.
4.22
35