不可或缺 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
[源码下载]

时间: 2024-08-04 22:22:01

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

不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自定义类型转换 示例CppOperator.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppOperator { public: string Demo(); }; } CppOperator.cpp /* * 运算符重载, 自定义类型转换 */ #include &qu

不可或缺 Windows Native 系列文章索引

[源码下载] 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Windows Native 之 C 语言 在 Windows Store Apps 中调用 C/C++ hello c 2.不可或缺 Windows Native (2) - C 语言: 常量,变量,基本数据类型 介绍不可或缺 Windows Native 之 C 语言 常量 变量 基本数据类型 3.不可或缺 Windows Native (3) - C 语言:

C++ 虚函数的缺省参数问题

前些日子,有个同学问我一个关于虚函数的缺省参数问题.他是从某个论坛上看到的,但是自己没想通,便来找我.现在分享一下这个问题.先看一小段代码: #include <iostream> using namespace std; class A { public: virtual void Fun(int number = 10) { cout << "A::Fun with number " << number; } }; class B: public

不可或缺 Windows Native (23) - C++: 虚函数

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 虚函数 示例1.基类CppHuman.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppHuman { protected: string Name; public: // 我是虚函数 virtual string Show(); // 我是纯虚函数(后面的“=0”只起形式上的作用,用

不可或缺 Windows Native (20) - C++: 友元函数, 友元类

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演示友元函数, 友元类CppClass4.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppClass4 { public: string Demo(); }; } CppClass4.cpp /* * 友元(friend)函数, 友元类 * * 友元函数: C+

不可或缺 Windows Native (6) - C 语言: 函数

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h #ifndef _MYHEAD_FUNCTION_ #define _MYHEAD_FUNCTION_ #ifdef __cplusplus extern "C" #endif // 函数声明 // 像这种在 .h 中声明的函数,如果想被外部文件调用的话,则外部文件不用再声明即可调用 char *demo_cFunction(); long recursion

不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, const 对象的引用

[源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象,  const 指针和指向 const 对象的指针, const 对象的引用 作者:webabcd 介绍不可或缺 Windows Native 之 C++ this 指针 对象数组 对象和指针 const 对象 const 指针和指向 const 对象的指针 const 对象的引用 示例1.CppEmployee 类CppEmployee.h #pragma

不可或缺 Windows Native (14) - C++: 文件

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 文件 示例CppIO2.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppIO2 { public: string Demo(string rootPath); }; } CppIO2.cpp /* * 文件 */ #include "pch.h" #include

不可或缺 Windows Native (12) - C++: 引用类型

[源码下载] 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 引用类型 示例CppReference.h #pragma once #include <string> using namespace std; namespace NativeDll { class CppReference { public: string Demo(); }; } CppReference.cpp /* * 引用类型 * * 引用也可以称之为“别名” * * 注: * 1.声明引