05学习笔记:C++引用以及函数提高

引用


引用的基本使用

作用:给变量起别名

语法:数据类型 &别名 = 原名

示例:
#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int &b = a;  //将变量a起别名为b
    b = 20;
    cout << a << endl;  //a = 20
	system("pause");
	return 0;
}
引用注意事项
  • 引用必须初始化
  • 引用在初始化后,不可以改变

示例:

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    int c = 20; 
    int &b = a;   //引用必须初始化,进行引用同时需要为其赋值
    // int &b;  //这种做法是错误的
    //b = c;  //b做为a的别名之后,不可以再更改为其他变量的别名,此操作为赋值操作
	system("pause");
	return 0;
}
引用做函数参数
  •    函数传参时,可以利用引用的技术让形参修饰实参
  • 优点:可以简化指针修改实参

示例:

#include <iostream>
using namespace std;
//1值传递
void mySwap01(int a,int b)
{
    int temp = a;
    a = b;
    b = temp;
    cout << swap01 a = << a << endl;
    cout << swap01 b = << b << endl;
}
//2地址传递
void mySwap02(int *a,int *b)
{
    int *temp = *a;
    *a = *b;
    *b = *temp;
}
//3引用传递
void mySwap03(int &a,int &b)
{
    int temp = a;
    a = b;
    b = temp;
}
int main()
{
    int a = 10;
    int b = 20;
    mySwap01(a,b);  //值传递 a和b不改变
    mySwap02(&a,&b);//地址传递 a和b改变
    mySwap03(a,b);//引用传递 a和b改变
	system("pause");
	return 0;
}
总结:通过引用参数产生的效果与地址传递是一样的。引用的语法更清楚简单
引用做函数返回值
  • 作用:引用是可以作为函数的返回值存在的
  • 注意:不要返回局部变量引用
  • 用法:函数调用作为左值

示例:

#include <iostream>
using namespace std;

//引用做函数的返回值
//1、不要返回局部变量的引用
int& test01()
{
    int a = 10; //局部变量存放在四区中的栈区
    return a;
    
}

//函数的调用可以作为左值
int& test02()
{
    static int a = 10; //静态变量,存放在全局区,全局区上的数据在程序结束后系统释放
    return a;
}
int main()
{
    int &ref = test01();
    cout << "ref = " << ref << endl; //第一次结果正确,是因为编译器做了保留
    cout << "ref = " << ref << endl; //第二次结果正确,因为a的内存已经释放

    int &ref2 = test02();
    cout << "ref2 = " << ref2 << endl;  //ref2 = 10
    cout << "ref2 = " << ref2 << endl;  //ref2 = 10
    test02() = 1000;  //如果函数的返回值是引用,这个函数调用可以作为左值
    cout << "ref2 = " << ref2 << endl;  //ref2 = 1000
    cout << "ref2 = " << ref2 << endl;  //ref2 = 1000

	system("pause");
	return 0;
}
引用的本质

本质:引用的本质在C++内部实现是一个指针常量。

讲解示例:

#include <iostream>
using namespace std;
//发现是引用,转换为int* const ref = &a;
void func(int& ref)
{
    ref = 100; //ref是引用,转换为*ref 100
}

int main()
{
    int a = 10;
    //自动转换为int *const ref = &a; 指针常量是指针指向不可改变,也说明为什么引用不可更改
    int& ref = a;
    ref = 20;    //内部发现ref是引用,自动转换为:*ref = 20
    
    cout << "a:" << a << endl;
    cout << "ref:" << ref << endl;

    func(a);
	system("pause");
	return 0;
}

结论:C++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都帮我们做了

常量引用
  • 作用:常量引用主要用来修饰形参,防止误操作
  • 在函数形参列表中,可以加const修饰形参,防止形参改变实参
#include <iostream>
using namespace std;

void showValue(const int& val)
{
	//val = 1000;  //用const修饰,防止误操作  
	cout << "val = " << val << endl;
}
int main()
{
	//常量引用
	//使用场景:用来修饰形参,防止误操作
	//int a = 10;

	//加上const之后,编译器将代码修改  int temp = 10; const int& ref = temp;

	//const int& ref = 10; //引用必须引一块合法的内存空间
	//ref = 20;  //加上const之后变为只读,不可修改
	int a = 100;
	showValue(a);
	system("pause");
	return 0;
}

函数提高


函数默认参数

在C++中,函数的形参列表中的形参是可以有默认值的。

语法:返回值类型 函数名(参数 = 默认值){ }

示例:

#include <iostream>
using namespace std;
//函数默认参数
//如果我们自己传入数据,就用自己的数据,如果没有,那么用默认值
//语法:返回值类型 函数名(形参 = 默认值){ }
int func(int a, int b = 20, int c = 30)
{
	return a + b + c;
}
//注意事项
//1、如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须都要有默认值
//2、如果函数声明有默认值,函数实现的时候就不能有默认参数
int func2(int a , int b);  //函数声明有默认参数
int func2(int a = 10,int b = 10)  //如果这里形参给默认值,会报错重定义默认参数
{
	return a + b;
}
int main()
{
	cout << func(10) << endl;
	cout << func2() << endl;

	system("pause");
	return 0;
}

函数占位参数

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

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

示例:

#include <iostream>
using namespace std;
//占位参数
//返回值类型 函数名(数据类型){ }
void func(int a, int = 10) //第二个int占位作用 占位参数可以有默认值
{
	cout << "this is func" << endl;
}
int main()
{
	func(10,10);  //占位参数如果有默认值,则实参可以不进行传递
	system("pause");
	return 0;
}

函数重载

  • 函数重载概述

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

函数重载满足条件:

1)同一个作用域

2)函数名称相同

3)函数参数类型不同 或者 个数不同 或者 顺序不同

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

示例:

#include <iostream>
using namespace std;
//函数重载
//可以让函数名相同,提高复用性

//函数重载的满足条件
//1、同一个作用域
//2、函数名相同
//3、函数参数类型不同 或 个数不同 或 顺序不同
void func(int c) 
{
	cout << "this is func" << endl;
}

void func(int a, int b)
{
	cout << "this is func!" << endl;
}

void func(double a, int b)
{
	cout << "this is func!!" << endl;
}

void func(int b, double a)
{
	cout << "this is func!!!" << endl;
}
int main()
{
	func(10);     //调用第一个函数
	func(10,10);  //调用第二个函数
	func(3.14,10);//调用第三个函数
	func(10,3.14);//调用第四个函数
	system("pause");
	return 0;
}
  • 函数重载注意事项

引用作为函数重载的条件

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

示例:

#include <iostream>
using namespace std;
//函数重载
//可以让函数名相同,提高复用性

//函数重载的注意事项
//1、引用作为重载的条件
void func(int& a)  //引用必要赋一块合法的内存空间
{
	cout << "this is func(int& a)" << endl;
}

void func(const int& a)
{
	cout << "this is func(const int& a)" << endl;
}
//2、函数重载碰到默认参数
void func2(int a,int b = 10)
{
	cout << "this is func2(int a,int b)" << endl;
}
void func2(int a)
{
	cout << "this is func2(int a)" << endl;
}
int main()
{
	int a = 10;
	func(a);     //调用第一个函数 a可读可写
	func(10);    //调用第二个函数   常量10 只可读
	func2(10,20);   //函数重载碰到默认参数,容易出现二义性
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值