C++中的引用和高级函数

引用

引用定义

作用:给变量起别名
语法:数据类型 &别名 = 原名

引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。
一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。

eg:

int i=20;
int &j=i;

引用注意事项

1.引用必须要初始化

int &a;//这是错误的使用方法

2.引用一旦初始化就不可以更改了;
3.、一个变量可取多个别名。

引用做函数参数

作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参。
我们以交换两个数的函数作为例子:

按c语言的方法我们需要进行传址操作

void Swap1(int*a,int*b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}
int main()
{
	int a, b;
	cin >> a ;
	cin >> b ;
	Swap1(&a, &b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

但我们学了引用后我们就可以引用传递

void Swap2(int& a, int& b)
{
	int temp = a;
	a = b;
	b = temp;
}
int main()
{
	int a, b;
	cin >> a ;
	cin >> b ;
	Swap2(a, b);
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	system("pause");
	return 0;
}

引用做函数的返回值

1.不要返回局部变量的引用

//切记此方法为错误用法
int& test()
{
	int a = 10;
	return a;
}

2.函数的调用可以作为左值

int& test()
{
	static int a = 10; //用static修饰延长函数的生命周期
	return a;
}
int main()
{
	int& a = test();
	cout << "a=" << a << endl;
	test() = 1000;//函数的调用可以作为左值
	cout << "a=" << a << endl;
	system("pause");
	return 0;
}

引用的本质

本质:引用本质在c++内部实现是一个指针常量(指针的指向是不可以改的,指向的值是可以改的)

#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	int& ref = a;//其内部相当于int*const ref = &a; 这也是引用不可更改的原因
	ref = 20;   //其内部相当于 *ref = 20;
	system("pause");
	return 0;
}

常量引用

作用:常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加const修饰形参,防止形参改变实参;
#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	const int& ref = a;
	a = 50;
	system("pause");
	return 0;
}

此时

a=100;

这样是可以的

ref=10;

是不合法的,是错误的,这样就可以防止误操作。

C++ 引用 vs 指针

引用很容易与指针混淆,它们之间有三个主要的不同:

1.不存在空引用。引用必须连接到一块合法的内存。
2.一旦引用被初始化为一个对象,就不能被指向到另一个对象。指针可以在任何时候指向到另一个对象。
3.引用必须在创建时被初始化。指针可以在任何时间被初始化。

函数提高

函数的的默认参数

在C++中,函数的形参列表中的形参是可以有默认值的。
语法: 返回类型    函数名  (参数 = 默认值)
         {
					函数体		
	     }

示例:

#include<iostream>
using namespace std;
int func(int a, int b=30, int c=20)
{
 return a + b + c;
}
int main()
{
 cout << func(10) << endl;
 system("pause");
 return 0;
}

输出结果:60
如果我们自己传入数据,就使用自己的数据,如果没有,那就用默认值。
如下:

#include<iostream>
using namespace std;
int func(int a, int b=30, int c=20)
{
	return a + b + c;
}
int main()
{
	cout << func(10,100) << endl;
	system("pause");
	return 0;
}

输出结果:130
注意事项:

1.如果某个位置已经有了,默认参数,那么从这个位置往后,从左往右都必须有默认值

如下:

#include<iostream>
using namespace std;
int func(int a, int b=30, int c,int d)   //这是错的
{
	return a + b + c;
}

上面代码就是错误案例。

2.如果函数声明有默参数,函数实现就不能有默认参数了
#include<iostream>
using namespace std;
int func(int a, int b=30, int c=20,int d=30);
int func(int a, int b ,int c,int d)   //此处就不能有默认参数了,因为声明的时候已经写了默认值参数值了
{
	return a + b + c;
}

错误案例:

#include<iostream>
using namespace std;
int func(int a, int b=30, int c=20,int d=30);
int func(int a, int b=30, int c=20,int d=30) //此处就是错误的
{
	return a + b + c;
}

函数的占位参数

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

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

如下:

#include<iostream>
using namespace std;
void func(int a, int)  //此时就出现的是占位参数
{
	cout << a << endl;
}
int main()
{
	func(10,30);
	system("pause");
	return 0;
}

占位参数也可以有默认参数
如下:

#include<iostream>
using namespace std;
void func(int a, int = 10)  //此时的占位参数就是一个默认参数
{
	cout << a << endl;
}
int main()
{
	func(10);
	system("pause");
	return 0;
}

函数重载

函数重载概述

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

函数重载满足条件:

1.同一个作用域下
2.函数名称相同
3.函数参数类型不同,或者个数不同或者顺序不同

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

示例:

#include<iostream>
using namespace std;
void func()
{
	cout << "func()的调用" << endl;
}
void func(int a)
{
	cout << "func(int a)的调用!" << endl;
}
int main()
{
	func();
	func(10);
	system("pause");
	return 0;

执行结果:
在这里插入图片描述
分析:
在这里插入图片描述
函数重载注意事项:

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

1.引用作为重载条件

#include<iostream>
using namespace std;
void func(int &a)
{
	cout << "func(int &a)的调用" << endl;
}
void func(const int& a)
{
	cout << "func(int a)的调用" << endl;
}

这样也是满足函数重载的
在后面主函数传参数,假如传一个变量

int a=10;
func(a)

这样会调用

void func(int &a)

如果传一个常量

func(10);

会调用

void func(const int& a)

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

#include<iostream>
using namespace std;
void func(int a,int b=10)
{
	cout << "func(int a, int b)的调用" << endl;
}
void func(int a)
{
	cout << "func(int a)的调用" << endl;
}
int main()
{
	int a = 10;
	func(a);   //此时编译器会报错,编译器会不知道调用哪一个函数
	system("pause");
	return 0;
}

所以,在我们使用函数重载时,要避免使用默认参数,避免歧义。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

binary~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值