不可或缺 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(); // function1_get 100 // 缺省参数 - 函数参数允许有缺省值,为某个参数提供缺省值后,则必须为其后面的参数提供缺省值
int function1_sum(int x, int y = , int z = );
int a = function1_sum(); //
int b = function1_sum(, ); //
int c = function1_sum(, , ); // 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 = , b = ;
int x; // 内联函数(内置函数,内嵌函数,inline) - 在编译时将所调用函数的代码直接嵌入到主调函数中
x = function2_max(a, b); // /*
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 + ;
} /*
这种写法是错误的,因为推导不出返回值的类型
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 = ; // 调用指定的函数,此函数有不定类型的参数
result = function2_template(i, j, ); // 101.1
}
OK
[源码下载]
不可或缺 Windows Native (16) - C++: 函数重载, 缺省参数, 内联函数, 函数模板的更多相关文章
- 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换
[源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...
- 不可或缺 Windows Native 系列文章索引
[源码下载] 不可或缺 Windows Native 系列文章索引 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Window ...
- 不可或缺 Windows Native (23) - C++: 虚函数
[源码下载] 不可或缺 Windows Native (23) - C++: 虚函数 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 虚函数 示例1.基类CppHuman ...
- 不可或缺 Windows Native (20) - C++: 友元函数, 友元类
[源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演 ...
- 不可或缺 Windows Native (6) - C 语言: 函数
[源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h # ...
- 不可或缺 Windows Native (13) - C++: 标准输入, 标准输出, 字符串内存流
[源码下载] 不可或缺 Windows Native (13) - C++: 标准输入, 标准输出, 字符串内存流 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 标准输 ...
- 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native
[源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:web ...
- 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...
- 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板
[源码下载] 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板 作者:webabcd 介绍不可或缺 Window ...
随机推荐
- JQuery checkbox check/uncheck
想通过JQuery来check或者uncheck页面上的checkbox控件,我们可能会想到用下面的代码: $('#chk-all').on('click', function(){ var chec ...
- Atitit 项目的主体设计与结构文档 v3
Atitit 项目的主体设计与结构文档 v3 1. 实现的目标2 1.1. cross device跨设备(pc 手机 平板)作为规划2 1.2. 企业级Java体系与开发语言2 1.3. 高扩展性, ...
- Atitit 图像处理知识点 知识体系 知识图谱v2
Atitit 图像处理知识点 知识体系 知识图谱v2 霍夫变换(Hough Transform) 霍夫变换是图像处理中从图像中识别几何形状的基本方法之一,应用很广泛,也有很多改进算法.主要用来从图像 ...
- js 四舍五入函数 toFixed(),小数位数精度
js的加减乘除有时得到的结果的小数的位数非常大,这种结果非常难以读取,例如某两个数相乘得到的结果是:1.3921000000000001 这种结果小数的位数有点多,一般需要的结果是四舍无入的 1.39 ...
- btn css
.searchButtonBtn { border: 0; padding: 0 23px; height: 27px; line-height: 27px; cursor: pointer; bac ...
- 为什么项目的jar包会和tomcat的jar包冲突?
为什么项目的jar包会和tomcat的jar包冲突? 碰到这个问题,猜测tomcat启动时会将自己的lib和项目的lib在逻辑上归并为一个大的lib,但是并没有做版本区分以及去重,这样相同的包可能就有 ...
- 开源IM工程“蘑菇街TeamTalk”的现状:一场有始无终的开源秀
1.前言 随着云IM的发展,已吸引越来越多有IM需求的APP接入.但考虑到云IM无论从商业模式还是运营模式上,还需经过多年的沉淀,才可能真正实现客户与服务商的运营和服务良性循环的双赢局面.在此之前,加 ...
- Java 线程 — ConcurrentHashMap
ConcurrentHashMap ConcurrentHashMap 结构 采用了分段锁的方法提高COncurrentHashMap并发,一个map里面有一个Segment数组--即多个Segmen ...
- 那些年我们写过的T-SQL(上篇)
在当今这个多种不同数据库混用,各种不同语言不同框架融合的年代(一切为了降低成本并高效的提供服务),知识点多如牛毛.虽然大部分SQL脚本可以使用标准SQL来写,但在实际中,效率就是一切,因而每种不同厂商 ...
- C# yeild使用
C# yeild的两种形式的yield语句: yield return <expression>; yield break; 使用 yield return 语句每一次返回每个元素. 将使 ...