【C/C++】函数重载注意事项

函数重载概述

函数重载满足条件:

  • 同一个作用域下
  • 函数名称相同
  • 函数参数类型不同 或者 个数不同 或者 顺序不同

注意: 函数的返回值不可以作为函数重载的条件。

函数重载注意事项

  • 引用作为重载条件
  • 函数重载碰到函数默认参数
c 复制代码
//函数重载注意事项
//1、引用作为重载条件

void func(int &a)	// int& ref = 10; 不合法
{
	cout << "func (int &a) 调用 " << endl;
}

void func(const int &a)	// 编辑器优化:int temp = 10; const int& a = temp;
{
	cout << "func (const int &a) 调用 " << endl;
}


//2、函数重载碰到函数默认参数

void func2(int a, int b = 10)
{
	cout << "func2(int a, int b = 10) 调用" << endl;
}

void func2(int a)
{
	cout << "func2(int a) 调用" << endl;
}

int main() {
	
	int a = 10;
	func(a); //调用无const
	func(10);//调用有const


	//func2(10); //碰到默认参数产生歧义,需要避免

	system("pause");

	return 0;
}
相关推荐
FuckPatience23 分钟前
Visual Studio C# 项目中文件后缀简介
开发语言·c#
老四啊laosi7 小时前
[C++进阶] 24. 哈希表封装unordered_map && unordered_set
c++·哈希表·封装·unordered_map·unordered_set
014-code7 小时前
订单超时取消与库存回滚的完整实现(延迟任务 + 状态机)
java·开发语言
妙为7 小时前
银河麒麟V4下编译Qt5.12.12源码
c++·qt·国产化·osg3.6.5·osgearth3.2·银河麒麟v4
lly2024067 小时前
组合模式(Composite Pattern)
开发语言
游乐码8 小时前
c#泛型约束
开发语言·c#
Dontla8 小时前
go语言Windows安装教程(安装go安装Golang安装)(GOPATH、Go Modules)
开发语言·windows·golang
chushiyunen8 小时前
python rest请求、requests
开发语言·python
铁东博客8 小时前
Go实现周易大衍筮法三变取爻
开发语言·后端·golang
baidu_huihui8 小时前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip