当前位置: 首页 > news >正文

函数的引用/函数的默认参数/函数的占位参数/函数重载

 函数的引用

#include<iostream>
using namespace std;

//引用的本质在c++内部实现,是一个指针常量

//交换函数
//1.值传递
void mySwap01(int a, int b) {
	int temp = a;
	a = b;
	b = temp;
}

//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;
}


//1.不要返回局部变量的引用
int& test01() {
	int a = 10;//局部变量,存放在栈
	return a;
}

//2.函数的调用可以作为左值
int& test02() {
	static int a = 10;//存放在全局区,在程序结束后系统释放
	return a;
}


//打印数据函数
void showValue(const int& value) {
	//value = 1000;  防止这种误操作
	cout << "value=" << value << endl;
}

int main() {
	int a = 10;
	//创建引用——起别名——别名的前面加上&——操作的都是同一块内存
	int& c = a;//引用必须要初始化;引用一旦初始化后,就不可以更改了
	c = 20;
	cout << "a=" << a << endl;
	cout << "------------------" << endl;


	//引用做函数参数
	a = 10;
	int b = 20;
	mySwap01(a, b);//值传递,形参不会修饰实参
	cout << "值传递  a=" << a << " b=" << b << endl;//a=10 b=20
	cout << "------------------" << endl;

	mySwap02(&a, &b);
	cout << "地址传递  a=" << a << " b=" << b << endl;
	cout << "------------------" << endl;

	mySwap03(a, b);
	cout << "引用传递  a=" << a << " b=" << b << endl;
	cout << "------------------" << endl;


	//引用做函数的返回值
	int& ref1 = test01();
	cout << "ref1=" << ref1 << endl;
	//cout << "ref1=" << ref1 << endl;

	int& ref2 = test02();
	cout << "ref2=" << ref2 << endl;
	cout << "------------------" << endl;


	//常量引用  const  用来修饰形参防止误操作
	
	const int& ref = 10;//引用必须引一块合法的内存空间

	a = 100;
	showValue(a);
	

	//system("pause");
	return 0;
}

函数的默认参数

#include<iostream>
using namespace std;

//函数的默认参数

//如果自己传入数据,就用自己的数据,如果没有就用默认值
//语法:返回值类型 函数名(形参=默认值){}
int func(int a, int b=20, int c=30) {
	return a + b + c;
}


//1.如果一个位置有了默认参数,那么从这个位置开始,从左到右都要有默认值
//2.如果函数声明有默认参数,函数实现就不能有默认参数;是现有声明就别有
int main() {
	cout << func(10) << endl;
	//system("pause");
	return 0;
}

函数的占位参数

#include<iostream>
using namespace std;

//占位参数
//返回值类型 函数名 (数据类型){}
//占位参数 还可以有默认参数
void func(int a, int =10) {//int 是占位用的
	cout << "this is func" << endl;
}
int main() {
	func(10);
	return 0;
}

函数重载

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

/*
函数重载的满足条件
1.同一个作用域下
2.函数名相同
3.参数类型不同,或个数不同,或顺序不同

注意:函数的返回值不可以作为函数重载的条件
*/
void func(int a) {
	cout << "func的调用" << endl;
}

void func() {
	cout << "func的调用!" << endl;
}
int main() {
	func();
	func(10);
	return 0;
}
#include<iostream>
using namespace std;
//函数重载
//可以让函数名相同,提高复用性

/*
函数重载的满足条件
1.同一个作用域下
2.函数名相同
3.参数类型不同,或个数不同,或顺序不同

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

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

void func() {
	cout << "func的调用!" << endl;
}


//函数重载的注意事项
//1.引用作为重载的条件

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



int main() {

	func();
	func(10);

	int a = 30;
	fun(a);//a可变
	fun(40);//40不可变
	return 0;
}

相关文章:

  • 面试vue2开发时怎么加载编译速度(webpack)
  • 用C++新建快捷方式
  • 第5章 构造、析构、拷贝语义学3:对象复制语意学
  • 高频面试题(含笔试高频算法整理)基本总结回顾24
  • 【ElasticSearch】学习笔记
  • 零基础上手Python数据分析 (3):Python核心语法快速入门 (下) - 程序流程控制、函数与模块
  • 用ST7789屏幕导致负片(反色)的问题
  • 基于DeepSeek R1的检验检查超声影像综合预约排班和路径最优化研究
  • yolo环境 pytorch环境配置 CUDA安装
  • Google最新生图模型Gemini-2.0-Flash-Exp免费用
  • 大华SDK协议在智联视频超融合平台中的接入方法
  • lws-minimal-ws-server前端分析
  • YOLO11 使用入门
  • Qt常用控件之Layout总篇
  • Python(学习一)
  • Mac 上编译 Ragflow
  • Manus 技术探索 - 使用 gVisor 在沙箱内运行 Ubuntu 容器并通过远程浏览器访问
  • 【A2DP】深入解读A2DP中通用访问配置文件(GAP)的互操作性要求
  • python速通小笔记
  • 关于单一职责原则
  • 陈逸飞《黄河颂》人物造型与借鉴影像意义
  • 香港发生车祸致22人受伤,4人伤势严重
  • 上海浪琴环球马术冠军赛明日启幕!五一假期在这里感受精彩
  • “五一”假期首日跨区域人员流动预计超3.4亿人次
  • 申活观察|咖香涌动北外滩,带来哪些消费新想象?
  • 来上海喝云南咖啡!上海国际咖啡文化节助力咖啡产业破圈出海