【C++基础】第三十七课:函数重载

函数重载

Posted by x-jeff on February 5, 2022

【C++基础】系列博客为参考《C++ Primer中文版(第5版)》C++11标准)一书,自己所做的读书笔记。
本文为原创文章,未经本人允许,禁止转载。转载请注明出处。

1.函数重载

如果同一作用域内的几个函数名字相同但形参列表不同,我们称之为重载(overloaded)函数。例如:

1
2
3
void print(const char *cp);
void print(const int *beg, const int *end);
void print(const int ia[], size_t size);

当调用这些函数时,编译器会根据传递的实参类型推断想要的是哪个函数:

1
2
3
4
int j[2] = {0,1};
print("Hello World");//调用print(const char*)
print(j, end(j)-begin(j));//调用print(const int*, size_t)
print(begin(j),end(j));//调用print(const int*, const int*)

⚠️main函数不能重载。

1.1.定义重载函数

对于重载的函数来说,它们应该在形参数量或形参类型上有所不同。

⚠️不允许两个函数除了返回类型外其他所有的要素都相同。假设有两个函数,它们的形参列表一样但是返回类型不同,则第二个函数的声明是错误的:

1
2
Record lookup(const Account&);
bool lookup(const Account&);//错误:与上一个函数相比只有返回类型不同

1.2.判断两个形参的类型是否相异

有时候两个形参列表看起来不一样,但实际上是相同的:

1
2
3
4
5
6
7
//每对声明的是同一个函数
Record lookup(const Account &acct);
Record lookup(const Account&);//省略了形参的名字

typedef Phone Telno;
Record lookup(const Phone&);
Record lookup(const Telno&);//Telno和Phone的类型相同

1.3.重载和const形参

顶层const不影响传入函数的对象。一个拥有顶层const的形参无法和另一个没有顶层const的形参区分开来:

1
2
3
4
5
Record lookup(Phone);
Record lookup(const Phone);//重复声明了Record lookup(Phone)

Record lookup(Phone*);
Record lookup(Phone* const);//重复声明了Record lookup(Phone*)

另一方面,如果形参是某种类型的指针或引用,则通过区分其指向的是常量对象还是非常量对象可以实现函数重载,此时的const是底层的:

1
2
3
4
5
6
7
//对于接受引用或指针的函数来说,对象是常量还是非常量对应的形参不同
//定义了4个独立的重载函数
Record lookup(Account&);//函数作用于Account的引用
Record lookup(const Account&);//新函数,作用于常量引用

Record lookup(Account*);//新函数,作用于指向Account的指针
Record lookup(const Account*);//新函数,作用于指向常量的指针

在上面的例子中,编译器可以通过实参是否是常量来推断应该调用哪个函数。因为const不能转换成其他类型,所以我们只能把const对象传递给const形参。相反的,因为非常量可以转换成const,所以上面的4个函数都能作用于非常量对象或者指向非常量对象的指针。不过,当我们传递一个非常量对象或者指向非常量对象的指针时,编译器会优先选用非常量版本的函数。

1.4.const_cast和重载

const_cast的使用:链接

1
2
3
4
const string &shorterString(const string &s1, const string &s2)
{
	return s1.size() <= s2.size() ? s1 : s2;
}

改写一个新的shorterString函数,当它的实参不是常量时,得到的结果是一个普通的引用,使用const_cast可以做到这一点:

1
2
3
4
5
string &shorterString(string &s1, string &s2)
{
	auto &r = shorterString(const_cast<const string&>(s1), const_cast<const string&>(s2));
	return const_cast<string&>(r);
}

1.5.调用重载的函数

函数匹配(function matching)是指一个过程,在这个过程中我们把函数调用与一组重载函数中的某一个关联起来,函数匹配也叫做重载确定(overload resolution)。编译器首先将调用的实参与重载集合中每一个函数的形参进行比较,然后根据比较的结果决定到底调用哪个函数。

当调用重载函数时有三种可能的结果:

  1. 编译器找到一个与实参最佳匹配(best match)的函数,并生成调用该函数的代码。
  2. 找不到任何一个函数与调用的实参匹配,此时编译器发出无匹配(no match)的错误信息。
  3. 有多于一个函数可以匹配,但是每一个都不是明显的最佳选择。此时也将发生错误,称为二义性调用(ambiguous call)

2.重载与作用域

一般来说,将函数声明置于局部作用域内不是一个明智的选择。但是为了说明作用域和重载的相互关系,我们将暂时违反这一原则而使用局部函数声明。

1
2
3
4
5
6
7
8
9
10
11
12
string read();
void print(const string &);
void print(double);//重载print函数
void fooBar(int ival)
{
	bool read=false;//新作用域:隐藏了外层的read
	string s=read();//错误:read是一个布尔值,而非函数
	void print(int);//新作用域:隐藏了之前的print
	print("Value: ");//错误:print(const string &)被隐藏掉了
	print(ival);//正确:当前print(int)可见
	print(3.14);//正确:调用print(int);print(double)被隐藏掉了
}

在fooBar内声明的print(int)隐藏了之前两个print函数,因此只有一个print函数是可用的:该函数以int值作为参数。

在C++语言中,名字查找发生在类型检查之前。

修改之后:

1
2
3
4
5
6
7
8
9
void print(const string &);
void print(double);
void print(int);
void fooBar2(int ival)
{
	print("Value: ");//调用print(const string &)
	print(ival);//调用print(int)
	print(3.14);//调用print(double)
}