13、C++函数重载(提高复用性)

一、函数重载满足条件:

1、同一作用域下

2、函数参数(类型不同 or 个数不同 or 顺序不同

//参数顺序,不同(参数间必须是不同类型
void func(double a, int b)
{
    cout << "func(double a,int b)的调用 4 " << endl;
}

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

二、注意事项:

1.引用作为函数重载的条件

int a=20;

func(a);        //调用func(int& a)

func(10);        //调用func(const int& a)

// int&  和  const int&  类型不同

//可以重载,参数类型不同
void func(int& a)   //int& a=10; ×   引用定义时初始化
{
    cout << "func(int& a) " << endl;
}

void func(const int& a)   //const int& a=10;  √ 碰到 const+& ,编译器自动创建temp,temp=10
{
    cout << "func(const int& a) " << endl;
}//---------------------------//正确

——————————————————————

//    intconst int   ??  

//函数主体一样,无法重载
//void func(int a)            // int a=10;
//{
//    cout << "func(int a) " << endl;
//}
//
//void func(const int a)                //const int a=10;
//{
//    cout << "func(const int a) " << endl;
//}-----------------------------//错误

2.函数重载碰到默认参数

//想函数重载,别加默认参数

func(10);   //×产生二义性

————————————————
void func2(int a,int b=10)    //函数重载,用了默认参数,容易有二义性
{
    cout << "func(int a,int b)" << endl;
}

————————————————

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

#include<iostream>
using namespace std;


//	int 和 const int   ??
//void func(int a)
//{
//	cout << "func(int a) " << endl;
//}
//
//void func(const int a)
//{
//	cout << "func(const int a) " << endl;
//}-----------------------------------------------------//错误


// int&  和  const int&  类型不同
void func(int& a)   //int& a=10; ×   引用定义时初始化
{
	cout << "func(int& a) " << endl;
}

void func(const int& a)   //const int& a=10;  √ 碰到 const+& ,编译器自动创建temp,temp=10
{
	cout << "func(const int& a) " << endl;
}//---------------------------------------------------//正确


//想函数重载,别加默认参数
void func2(int a,int b=10)    //函数重载,用了默认参数,容易有二义性
{
	cout << "func(int a,int b)" << endl;
}

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

int main()
{
	int a = 10;
	const int b = 20;

	func(a);      //func(int& a),a是变量  //int& a=10;		错
	
	func(b);      //func(const int& a),b是const int   const int&a=10;  对

	func(30);     //func(const int& a),10是常量

	int c = 30;   //引用本质:指针常量
	int& d = c;   //int* const d=&c;    
	func(d);

	int ct = 30;		  
	const int& dt = ct;   //常量引用,防止修改ct    int -> const int&
	func(dt);

	//const int cf = 30;
	//int& df = cf;     //错,               无法由 const int -> int&
	//func(df);

	//func2(10);   出错,有二义性
	func2(10, 20);

	system("pause");
	return 0;
}

指针,重载函数中(const int*  和  int*)一样可以区别


#include<iostream>
using namespace std;

void func(int* p)
{
	cout << "func(int*p)" << endl;
}

void func(const int* p)
{
	cout << "const func(int* p)" << endl;
}

int main()
{
	int a = 10;
	func(&a);        //func(int* p)

	const int b = 20;
	func(&b);        //const func(int* p)

	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值