【C++】函数重载 - 给代码增添多彩的魔法

        欢迎来到博主 Apeiron 的博客,祝您旅程愉快 ! 时止则止,时行则行。动静不失其时,其道光明。


目录

1、缘起

2、函数重载概述

3、函数重载注意事项 

4、总结


1、缘起

        函数重载,是编程世界中的一抹迷人色彩,让代码焕发出无限魅力。它犹如一位魔法师,能够在代码的舞台上施展出奇妙的变幻。当我们面对不同的情景和需求时,函数重载能够以其神奇的力量,根据参数 类型数量 顺序,自如地选取最合适的法术。就像一本魔法书中的不同章节,每一次调用函数都是在翻阅其中的一页。

        函数重载是一种强大的工具,它不仅能提高代码的可读性,还能让程序员的创造力得到充分的发挥。无论是在 数学计算图形处理 还是 游戏开发 中,函数重载都扮演着重要的角色。它像一枚瑰丽的宝石,闪耀着不同的光芒,为我们的代码增添了多样的可能性。

        函数重载,让我们的代码变得生动有趣。让我们摆脱单调和局限,用它的魔力创造出令人惊叹的程序。让我们一起享受函数重载带来的神奇吧!

2、函数重载概述

作用:函数名可以相同,提高函数的复用性

函数重载满足的条件:

①  同一个作用域下

②  函数名称相同

③  函数参数 类型不同 或者 数量不同 或者 顺序不同

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

示例代码:

#include<iostream>

using namespace std;

//函数声明
void test();
void test(int a);
void test(double a);
void test(int a, double b);
void test(double b, int a);

int main()
{
	test();

	//1、函数参数类型不同
	test(10);
	test(3.14);

	//2、函数参数个数不同
	test(10, 3.14);

	//3、函数参数顺序不同
	test(3.14, 10);
	
	cout << endl;
	system("pause");
	return 0;
}

void test()
{
	cout << "test() 的调用!" << endl;
}

void test(int a)
{
	cout << "test(int a) 的调用!" << endl;
}

void test(double a)
{
	cout << "test(double a) 的调用!" << endl;
}

void test(int a,double b)
{
	cout << "test(int a,double b) 的调用!" << endl;
}

void test(double b, int a)
{
	cout << "test(double b,int a) 的调用!" << endl;
}

3、函数重载注意事项 

①  引用作为重载条件

示例代码 1:

#include<iostream>

using namespace std;

//函数声明
void test(int& a);
void test(const int& a);

int main()
{
	int a = 10;

	test(a);  //调用无 const

	cout << endl;
	system("pause");
	return 0;
}

//引用作为重载条件
void test(int& a)
{
	cout << "test(int& a)调用" << endl;
}

void test(const int& a)
{
	cout << "test(const int& a)调用" << endl;
}

注意:const 修饰的变量是一个 const 常量,因为 test(a) 的实参是一个变量,所以 test(a) 调用 void test(int& a) 这个函数。

示例代码 2: 

#include<iostream>

using namespace std;

//函数声明
void test(int& a);
void test(const int& a);

int main()
{
	test(10);  //调用有 const
	
	cout << endl;
	system("pause");
	return 0;
}

//引用作为重载条件
void test(int& a)  //int &a = 10; 不合法
{
	cout << "test(int& a)调用" << endl;
}

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

注意:const 修饰的变量是一个 const 常量,因为 test(10) 的实参是一个常量,所以 test(10) 调用 void test(const int& a) 这个函数。 

②  函数重载碰到函数默认参数

示例代码:

#include<iostream>

using namespace std;

//函数声明
void test(int a, int b);
void test(int a);

int main()
{
	test(10);
	
	cout << endl;
	system("pause");
	return 0;
}

//函数重载碰到函数默认参数
void test(int a, int b = 20)  
{
	cout << "test(int a, int b)调用" << endl;
}

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

注意:如图所示,函数 test(10) 出现报错。因为 void test(int a, int b = 20)  函数中有默认参数 int b = 20,所以 test(10) 函数既可以调用 void test(int a)  函数,也可以调用 void test(int a, int b = 20)  函数。即在这种情况下,函数调用就出现了 二义性,在实际情况中,尽量避免这种情况的发生。

4、总结

        当我们使用函数重载时,就像是和魔法师一起创造魔法般的代码。我们可以传递不同类型的参数,从而调用不同版本的函数,使程序变得灵活而精彩。函数重载是代码世界的魔杖,它引领我们进入一个充满创意和惊喜的奇幻世界。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aperion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值