Skip to content

函数重载

函数重载允许我们使用多个同名函数。

但也有一些使用限制:

  1. 重载条件:函数名相同,但参数列表必须不同(类型、数量或顺序)
  2. 返回类型:返回类型不同是没用的,必须参数列表不同

函数重载使得函数名可复用,减少程序员在起名时浪费的脑细胞。

cpp
#include <iostream>
using namespace std;

// 1. 参数类型不同的重载
void print(int value) {
    cout << "整数: " << value << endl;
}

void print(double value) {
    cout << "浮点数: " << value << endl;
}

void print(const string& text) {
    cout << "字符串: " << text << endl;
}

// 2. 参数数量不同的重载
int add(int a, int b) {
    return a + b;
}

int add(int a, int b, int c) {
    return a + b + c;
}

// 3. 参数顺序不同的重载
void display(int a, double b) {
    cout << "整数: " << a << ", 浮点数: " << b << endl;
}

void display(double a, int b) {
    cout << "浮点数: " << a << ", 整数: " << b << endl;
}

int main() {
    // 调用不同的重载函数
    print(10);           // 调用 print(int)
    print(3.14);         // 调用 print(double)
    print("Hello");      // 调用 print(const string&)
    
    cout << "两数之和: " << add(5, 10) << endl;
    cout << "三数之和: " << add(5, 10, 15) << endl;
    
    display(10, 3.5);    // 调用 display(int, double)
    display(3.5, 10);    // 调用 display(double, int)
    
    return 0;
}
cpp
#include <iostream>
using namespace std;

// 4. 带有默认参数的重载
void show(int a) {
    cout << "show(int): " << a << endl;
}

void show(int a, int b = 0) {
    cout << "show(int, int): " << a << ", " << b << endl;
}

// 5. 与const结合的重载
class MyClass {
public:
    // const成员函数重载
    void display() {
        cout << "非const成员函数" << endl;
    }
    
    void display() const {
        cout << "const成员函数" << endl;
    }
    
    // 参数为指针/引用的重载
    void process(int* ptr) {
        cout << "处理指针: " << *ptr << endl;
    }
    
    void process(int& ref) {
        cout << "处理引用: " << ref << endl;
    }
};

// 6. 函数模板重载
template<typename T>
T maxValue(T a, T b) {
    return (a > b) ? a : b;
}

// 特化版本
template<>
const char* maxValue<const char*>(const char* a, const char* b) {
    return (strcmp(a, b) > 0) ? a : b;
}

// 普通函数与模板函数重载
int maxValue(int a, int b) {
    cout << "调用普通函数" << endl;
    return (a > b) ? a : b;
}

int main() {
    // 测试const重载
    MyClass obj1;
    const MyClass obj2;
    
    obj1.display();   // 调用非const版本
    obj2.display();   // 调用const版本
    
    // 测试指针/引用重载
    int value = 42;
    obj1.process(&value);  // 调用指针版本
    obj1.process(value);   // 调用引用版本
    
    // 测试模板重载
    cout << "最大值: " << maxValue(10, 20) << endl;      // 调用普通函数
    cout << "最大值: " << maxValue(3.14, 2.71) << endl;  // 调用模板函数
    
    return 0;
}
cpp
#include <iostream>
using namespace std;

// 7. 不能仅通过返回类型区分重载
/*
int getValue() {    // 错误:与下面的函数仅返回类型不同
    return 10;
}

double getValue() { // 错误:与上面的函数仅返回类型不同
    return 10.5;
}
*/

// 8. 注意隐式类型转换可能导致的二义性
void process(long a) {
    cout << "处理long类型: " << a << endl;
}

void process(double a) {
    cout << "处理double类型: " << a << endl;
}

// 9. 重载解析示例
void example(int a, int b = 0) {
    cout << "example(int, int): " << a << ", " << b << endl;
}

void example(double a) {
    cout << "example(double): " << a << endl;
}

void example(char a, int b = 0) {
    cout << "example(char, int): " << a << ", " << b << endl;
}

int main() {
    // 测试二义性问题
    // process(10);  // 错误:10可以转换为long或double,产生二义性
    
    // 明确调用
    process(10L);     // 调用long版本
    process(10.0);    // 调用double版本
    
    // 重载解析
    example(10);      // 调用 example(int, int)
    example(10.0);    // 调用 example(double)
    example('A');     // 调用 example(char, int)
    example(10.5f);   // 调用 example(double) - float提升为double
    
    return 0;
}