(以下根据MSDN整理而成)
Overloaded functions differentiate between argument types that take different initializers. Therefore, an argument of a given type and a reference to that type are considered the same for the purposes of overloading. They are considered the same because they take the same initializers. For example, max( double, double ) is considered the same as max( double &, double & ). Declaring two such functions causes an error.
重载函数根据不同的参数初始化方式区分函数。因此,某个类型的参数和这个类型的引用参数被认为一样的,因为他们的初始化方式一致。所以,max( double, double ) 被认为和max( double &, double & ).一样。
For the same reason, function arguments of a type modified by const or volatile are not treated differently than the base type for the purposes of overloading.
同样原因,某个类型的参数和这个类型的const参数也认为是一样的。
However, the function overloading mechanism can distinguish between references that are qualified by const and volatile and references to the base type. This makes code such as the following possible:
不过,函数重载机制可以区分引用和const、volatile引用。因此,下面代码可行。
// argument_type_differences.cpp
// compile with: /EHsc /W3
// C4521 expected
#include <iostream>
using namespace std;
class Over {
public:
Over() { cout << "Over default constructor\n"; }
Over( Over &o ) { cout << "Over&\n"; }
Over( const Over &co ) { cout << "const Over&\n"; }
Over( volatile Over &vo ) { cout << "volatile Over&\n"; }
};
int main() {
Over o1; // Calls default constructor.
Over o2( o1 ); // Calls Over( Over& ).
const Over o3; // Calls default constructor.
Over o4( o3 ); // Calls Over( const Over& ).
volatile Over o5; // Calls default constructor.
Over o6( o5 ); // Calls Over( volatile Over& ).
}
Pointers to const and volatile objects are also considered different from pointers to the base type for the purposes of overloading.
另外,指针和const、volatile指针也被认为是不一样的。
总结一句话,重载时根据函数参数数量、类型进行区分,当const、volatile限定引用和指针类型的参数时,也可做区分的依据。