20240412,引用,函数高级

目录

一,引用

1.1 引用做函数参数——形参会修饰实参

1.2 引用做函数返回值

1.3 本质——指针常量

1.4 常量引用

二,函数提高

2.1 函数默认参数

2.2 函数占位参数

2.3 函数重载

2.4 函数重载注意事项

一,引用

给变量起别名,数据类型 & 别名=原名;引用一定要初始化,初始化之后不能更改

#include <iostream>
using namespace std;
int main()
{
	int a =10;
	int& b = a;
	//一定要初始化,是不能int&b;
	cout << a << endl;
	cout << b << endl;
	b = 99;
	cout << a << endl;
	cout << b << endl;
	a = 4445;
	cout << a << endl;
	cout << b << endl;
	int c = 20;
	b = c;
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
	c = 888;
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;

	return 0;
	system("pause");
}
1.1 引用做函数参数——形参会修饰实参
#include <iostream>
using namespace std;
void swap(int& a, int& b)
{
	a += b;
	b = a - b;
	a -= b;
	cout << a << endl;
	cout << b << endl;
}
int main()
{
	int a = 111;
	int b = 666;
	int& aa = a;
	int& bb = b;
	swap(aa, bb);
	cout << a << endl;
	cout << b << endl;

	return 0;
	system("pause");
}
1.2 引用做函数返回值

不要返回局部变量的引用/指针【栈区,会被释放】,函数的调用可以作为左值

#include <iostream>
using namespace std;
int& swap(int& a, int& b)//返回引用,视作返回地址/指针
{
	//static int& c = a + b;非常量应用的初始值必须做左值?
	//int sum = a + b;//静态变量,全局区,程序结束系统释放
	//static int& c = sum;//不对,静态引用什么鬼,哈哈哈哈哈哈哈,地址指向的还是局部变量
	static int sum = a + b;
	int& c = sum;
	return sum;
}
int main()
{
	int a = 111;
	int b = 666;
	int& aa = a;
	int& bb = b;
	int &c =swap(aa, bb);//C是这个函数返回值的引用,且不可更改
	cout << a << endl;
	cout << b << endl;
	cout << endl;

	cout << c << endl;
	cout << c << endl;
	cout << endl;

	swap(aa, bb) = 10000;//左值//给函数赋值->给C赋值
	cout << c << endl;
	cout << c << endl;
	return 0;
	system("pause");
}
1.3 本质——指针常量

乐,记不住专用术语系列。int &a=int *const p,差别就是引用看不到过程

1.4 常量引用

变量传入函数,可以用引用的方式接受

#include <iostream>
using namespace std;
void showvalue(/*const*/ int& val)//防修改
{
	cout << val << endl;
	val = 100;
}
int main()
{
	int a = 10;
	const int& c = 10;//引用必须是一块合法的空间,这里编译器将代码修改为,int temp=10;【一个零时变量?】const int&c=temp
	//c=23;//const了哦

	showvalue(a);//变量传入函数,用引用的方式接受
	cout << a << endl;
	return 0;
	system("pause");
}

二,函数提高

2.1 函数默认参数

返回值类型 函数名 (参数=默认值){语句};形参可以有默认值

#include <iostream>
using namespace std;
int show(int a, int b = 10, int c = 30)
//如果某个位置已经有了默认参数,那么从这个位置往后,从左往右必须都有默认值
{
	return a + b + c;
}
int func(int a, int  b);//声明和函数只要一个有默认参数
int func(int a = 190, int b = 10)
{
	return a + b;
}
int main()
{
	
	cout << show(20)<< endl;
	cout << show(20,20) << endl;//覆盖默认值
	//cout << show(20, ,20) << endl;语法错误
	cout << show(20,20,40) << endl;
	cout << endl;
	cout << func() << endl;
	return 0;
	system("pause");
}
2.2 函数占位参数

返回类型 函数名 (函数类型/INT){ };占位,调用函数时须填补该位置

#include <iostream>
using namespace std;

int func(int a , int )
{
	return a ;
}
int func2(int a, int = 10)//占位参数可以有默认值
{
	return a;
}
//int func1(int a = 190, int)不可以
//{
//	return a;
//}
int main()
{
	//cout << func1(1) << endl;
	//cout << func1(1,10) << endl;
	//都不行
	cout << func(1, 10) << endl;//10拿不到
	cout << func2(1, 10) << endl;
	return 0;
	system("pause");
}
2.3 函数重载

作用:函数名可以相同
条件:同一作用域,函数名称相同,函数参数类型 || 个数 || 顺序 (不同类型)不同  *函数的返回值不可以作为函数重载的条件

#include <iostream>
using namespace std;

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

//void func( int b, int a)
//{
//	cout << "函数调用( int b, int a)" << endl;
//}不行
//int func(int a, int b)
//{
//	cout << "函数调用(int a,int b)" << endl;
//}不行

int main()
{
	func();
	func(10);
	func(2.3);
	func(10, 10);
	func(10, 3.4);
	func(3.4, 10);
	return 0;
	system("pause");
}
2.4 函数重载注意事项

引用作为重载条件:原来值传递就是,真的进行了一次赋值操作,乐,懂了
重载函数遇到默认参数:保证【函数名+参数】只能启动一个函数

#include <iostream>
using namespace std;

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

//以下都不可以和 void func(int a)一起
//void func(const int a)
//{
//	cout << "函数调用(const int a)" << endl;
//}不行

//void func(int& a)
//{
//	cout << "函数调用(int& a)" << endl;
//}对重载函数的调用不明确?不行

void func2(int a, int b = 10)
{
	cout << "函数调用(int a, int b = 10)" << endl;
}
void func2(int a)
{
	cout << "函数调用(int a)_--2" << endl;
}
int main()
{
	int a = 10;
	func(a);
	func(10);
	const int b = 90;
	func(b);

	func2(10, 20);
	//func2(10);不行
	return 0;
	system("pause");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值