zoukankan      html  css  js  c++  java
  • 不可或缺 Windows Native (16)

    [源码下载]


    不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板



    作者:webabcd


    介绍
    不可或缺 Windows Native 之 C++

    • 函数重载
    • 缺省参数
    • 内联函数
    • 函数模板



    示例
    1、函数重载, 缺省参数
    CppFunction1.h

    #pragma once 
    
    #include <string>
    
    using namespace std;
    
    namespace NativeDll
    {
        class CppFunction1
        {
        public:
            string Demo();
        };
    }

    CppFunction1.cpp

    /*
     * 函数重载,缺省参数
     */
    
    #include "pch.h" 
    #include "CppFunction1.h" 
    #include "cppHelper.h" 
    
    using namespace NativeDll;
    
    string CppFunction1::Demo()
    {
        // 函数重载(overload) - 根据参数类型和个数做函数重载(不能仅按返回值类型来做函数重载)
        string function1_get();
        string function1_get(string s1);
        string function1_get(int i1);
        string result1 = function1_get(); // function1_get
        string result2 = function1_get("abc"); // function1_get abc
        string result3 = function1_get(100); // function1_get 100
    
    
        // 缺省参数 - 函数参数允许有缺省值,为某个参数提供缺省值后,则必须为其后面的参数提供缺省值
        int function1_sum(int x, int y = 1, int z = 2);
        int a = function1_sum(100); // 103
        int b = function1_sum(100, 100); // 202
        int c = function1_sum(100, 100, 100); // 300
    
    
        return "看代码及注释吧";
    }
    
    
    // 如果函数参数有缺省值,且有函数声明的话,则缺省值应该在函数声明处指定(此时在函数定义处指定函数缺省值是无效的)
    int function1_sum(int x, int y, int z) 
    { 
        return x + y + z; 
    }
    
    
    // 以下几个函数用于演示函数重载
    string function1_get()
    {
        return "function1_get";
    }
    /*
    不能仅按返回值类型来做函数重载(有这句的话会编译错误)
    int function1_get()
    {
        return 100;
    }
    */
    string function1_get(string s1)
    {
        return "function1_get " + s1;
    }
    string function1_get(int i1)
    {
        return "function1_get " + int2string(i1);
    }

     
    2、内联函数, 函数模板
    CppFunction2.h

    #pragma once 
    
    #include <string>
    
    using namespace std;
    
    namespace NativeDll
    {
        class CppFunction2
        {
        public:
            string Demo();
        };
    }

    CppFunction2.cpp

    /*
     * 内联函数,函数模板
     */
    
    #include "pch.h" 
    #include "CppFunction2.h" 
    
    using namespace NativeDll;
    
    void function2_demo1();
    void function2_demo2();
    
    string CppFunction2::Demo()
    {
        // 内联(inline)函数
        function2_demo1();
    
        // 函数模板(function template)
        function2_demo2();
    
        return "看代码及注释吧";
    }
    
    
    
    // 声明一个 inline 函数(左侧加 inline)
    inline int function2_max(int i, int j);
    
    // 内联(inline)函数的使用
    void function2_demo1()
    {
        int a = 1, b = 2;
        int x;
    
        // 内联函数(内置函数,内嵌函数,inline) - 在编译时将所调用函数的代码直接嵌入到主调函数中
        x = function2_max(a, b); // 2
    
        /*
        inline 函数会在编译时直接替换(类似宏替换),上面调用了 inline 函数,在编译时会被替换为如下代码
        if (a > b)
            x = a;
        x = b;
        */
    }
    
    // 定义一个 inline 函数(左侧加 inline)
    // 注意:从 inline 的原理看,其是以代码膨胀(复制)为代价,省去了函数调用的开销,从而提高函数的执行效率
    // 1、当函数包含复杂的控制语句,如循环语句或 switch 语句或递归之类的时,不宜用 inline 函数
    // 2、一般只将规模很小(5 句以下)且使用频繁的函数声明为 inline
    // 3、总之,如果不值当的(执行效率提高小,代码膨胀大),建议不用 inline
    inline int function2_max(int i, int j) 
    {
        if (i > j)
            return i;
        return j;
    }
    
    
    
    // 声明一个函数模板,其有两个不定类型,分别为 T1 和 T2(typename 和 class 没有区别)
    template<typename T1, class T2>
    
    // 使用上面的函数模板中定义的类型,定义一个函数
    T1 function2_template(T1 a, T2 b, int c) // 使用了函数模板的函数就是模板函数
    {
        if (a > b)
            return a;
        return a + 100;
    }
    
    /*
    这种写法是错误的,因为推导不出返回值的类型
    T2 function2_template(T1 a, T1 b)
    {
        if (a > b)
            return 100
        return 1000;
    }
    */
    
    void function2_demo2()
    {
        float result;
        float i = 1.1f;
        int j = 2;
    
        // 调用指定的函数,此函数有不定类型的参数
        result = function2_template(i, j, 0); // 101.1
    }



    OK
    [源码下载]

  • 相关阅读:
    【转载】RNA-seq测序方法
    NeuN神经元核抗原
    单细胞测序
    安装cytoscape时的折腾
    linux文件目录
    结合自己做过的数据,对cosmic signature理解
    减数分裂meiosis-有丝分裂miosis-染色体-DNA-等位基因
    AD发生了什么
    FDR的含义
    置换检验—结合GSEA解释
  • 原文地址:https://www.cnblogs.com/webabcd/p/4560130.html
Copyright © 2011-2022 走看看