函数的相关知识

函数的占位参数

C++中函数的形参可以有占位参数,用来做占位,调用函数时必须填补该位置

语法:

返回值类型 函数名 (数据类型){}

#include<iostream>
using namespace std;

void show(int a, int) 
//void show(int a,int = 10)//占位参数还可以有默认值,这时主函数里面可以不用赋值
{
	cout<<"this is show" << endl;
}
int main() {

	show(10, 10);

	system ("pause");
	return 0;
}

函数重载的注意事项:

1.引用作为重载的条件

#include<iostream>
using namespace std;

void show(int &a) {
	cout<<"int &a" << endl;
}
void show(const int& a) {
	cout << "const int &a" << endl;
}
int main() {

	int a = 10;
	show(a);//这个a是可读可写的状态,默认show(int &a)

	
	//const会制造一个临时的数据,让&a指向临时的空间,合法,所以执行下面show(const int &a)
	show(10);

	system ("pause");
	return 0;
}

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

#include<iostream>
using namespace std;

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

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

int main() {

	show(10);//此时出现了二义性,我们要尽量避免这种情况

	system ("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值