• C++ 函数


    1. 函数的默认参数

    // 默认参数
    // 1. 如果某个位置已经有了默认参数,那么从这个位置往后,从左到右都必须有默认值
    int test20(int a, int b, int c = 1, int d = 1)
    {
        return a + b + c + d;
    }
    
    // 2.声明和实现,只能有一个有默认参数
    //   如果函数的声明有默认参数,那么函数的实现就不能有默认参数
    //   如果函数函数的实现有默认参数,那么函数的声明就不能有默认参数
    
    int test21(int a, int b, int c = 0);
    
    int test21(int a, int b , int c)
    {
        return a + b + c;
    }
    
    int test22(int a, int b, int c);
    
    int test22(int a, int b , int c = 1)
    {
        return a + b + c;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2. 函数的占位参数

    // 占位参数
    void test23(int a, int)
    {
        cout << "test23" << endl;
    }
    
    int main(int argc, const char * argv[]) {
        test23(10, 1); // 调用必须传2个参数
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. 函数的重载

    // 函数的重载
    // 作用:函数名可以相同,提高复用性
    // 函数重载满足条件
    /**
     1. 同一个作用域下
     2. 函数名称相同
     3. 函数参数类型不同 或者 个数不同  或者顺序不同
     注意: 函数的返回值不可以作为函数重载的条件
     */
    
    void test24(int a, double b)
    {
        cout << "test24--1" << endl;
    }
    
    void test24(int a, double b, double c)
    {
        cout << "test24--2" << endl;
    }
    
    void test24(double b ,int a)
    {
        cout << "test24--3" << endl;
    }
    
    /* 返回值类型不同,不能作为函数重载条件
    int test24(double b ,int a)
    {
        cout << "test24--3" << endl;
    }*/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    4. 函数重载注意事项

    // 函数重载注意事项
    // 1. 引用作为重载条件
    
    void test25(int &a)
    {
        cout << "test25" << endl;
    }
    
    void test25(const int &a)
    {
        cout << "const -- test25" << endl;
    }
    
    // 2. 函数重载碰到默认参数
    void test26(int a, int b = 1)
    {
        cout << "test26" << endl;
    }
    
    void test26(int a)
    {
        cout << "test26" << endl;
    }
    int main(int argc, const char * argv[]) {
        
        int a = 1;
        int &r_a = a;
        const int &c_r_a = a;
        
        r_a = 2;
        
        // Tips:
        cout << "a = " << a << endl; // 2
        cout << "r_a = " << r_a << endl; // 2
        cout << "c_r_a = " << c_r_a << endl; // 2
        
        test25(a); // test25
        /**
         为什么 test25(10) 调用的是 :void test25(const int &a)
         因为:如果调用void test25(int &a),那么int &a = 10; 不合法
         所以:调用void test25(const int &a),const int &a = 10; 是合法的,会转为 int temp = 10; const int &a = temp;
         */
        test25(10); // const -- test25
        test25(r_a); // test25
        test25(c_r_a);// const -- test25
        
    //    test26(1); // 函数带有默认参数,调用两个都合法,会导致的二义性, 所以碰到默认参数,有时候是不能函数重载的
        
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
  • 相关阅读:
    使用浏览器这么多年,你真的了解DevTools吗?
    使用 LLaMA-Factory 实现对大模型函数调用功能
    08丨键入网址再按下回车,后面究竟发生了什么?
    MySQL中的索引
    MySQL主从同步,出现Slave_SQL_Running:no和slave_io_running:no问题的解决方法
    .NET 8 候选版本 2 (RC2) 现已可用
    盘点CSV文件在Excel中打开后乱码问题的三种处理方法
    synchronized 和 single 之间的故事
    评估指标与评分(上):二分类指标
    Python实现线性判别分析教程
  • 原文地址:https://blog.csdn.net/JH_Cao/article/details/126368982