1.作用域必须相同

2.函数名称相同

3.函数参数的类型不同,或个数不同,或顺序不同

void w3cTest(int a) { 
  cout << a;
}

这只对整型参数有效。重载它将使其可用于其他类型,如浮点型。

void w3cTest(float a) { 
  cout << a;
}

关于C++函数可以查看之前的文章:
https://www.5yang.cc/5918

4."引用作为重载函数的参数时需要注意的事项

#include<iostream>
using namespace std;

//引用作为重载函数的参数注意事项
void func(int &a) {
    cout << "int a" << endl;

}

void func(const int &a) {
    cout << "const int a" << endl;
}
int main() {
    int a = 10;
    func(a);  //调用重载函数,它会调用第一个 int a 而不是 const int a
    //如果你要调用const的重载函数,只需要 func(10); 给他传入一个const常量即可
}
最后修改:2022 年 12 月 05 日
如果觉得我的文章对你有用,请随意赞赏