C++ 函数重载

2018-11-14  本文已影响4人  CoderGuogt

规则

  1. 函数名相同
  2. 参数个数不同,参数类型不同,参数顺序不同(参数类型不相同的前提下)
void display(int a) {
    
    cout << "display(int a)" << endl;
}

void display(int a, int b) {
    
    cout << "display(int a, int b)" << endl;
}

void display(int a, double b) {
    
    cout << "display(int a, double b)" << endl;
}

void display(double a, int b) {
    
    cout << "display(double a, int b)" << endl;
}

main函数方法调用

display(3);
display(2, 3);
display(2.0f, 3);
display(2, 3.0f);

输出结果:

display(int a)
display(int a, int b)
display(double a, int b)
display(int a, double b)

注意

  1. 返回值类型与函数重载无关
  2. 调用函数时,实参的隐式类型转换可能会产生二义性

本质

上一篇 下一篇

猜你喜欢

热点阅读