【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;
}
相关推荐
天佑木枫1 分钟前
15天Python入门系列 · 序
开发语言·python
宋拾壹1 小时前
同时添加多个类目
android·开发语言·javascript
凡人叶枫1 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
不想写代码的星星2 小时前
std::move 根本不移动,就像老婆饼里没有老婆
c++
redaijufeng2 小时前
C++雾中风景7:闭包
c++·算法·风景
小小龙学IT2 小时前
Go 语言后端开发:从并发模型到生产落地的工程实践
开发语言·后端·golang
努力攻坚操作系统2 小时前
编程语言编译运行机制对比:C / Java / Python
java·c语言·python
ytttr8732 小时前
Qt 数字键盘实现
开发语言·qt
wearegogog1232 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
再写一行代码就下班2 小时前
Cursor配置Java环境、创建Spring Boot项目的步骤
java·开发语言·spring boot