C++(函数重载二)

函数重载注意事项

  • 1.引用作为重载条件
  • 2.函数重载碰到默认参数

示例:

//函数重载注意事项
//1、引用作为重载条件
#include <iostream>
using namespace std;

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

void func(const int &a)
{
	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;
}
原文地址:https://www.cnblogs.com/xuelanga000/p/13574139.html