文章目录

🥮一、缺省参数
🍕1、基本概念
🌰举个栗子👇
//缺省参数
void Func(int a = 1)
{
cout << a << endl;
}
int main()
{
Func(10);
Func();
return 0;
}
🍕2、缺省参数的分类
🚩全缺省参数
🌰举个栗子👇
//全缺省参数
void Func(int a = 10, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
}
int main()
{
Func();
//显示传参,从左往右显示传参
Func(1);
Func(1,2);
Func(1, 2, 3);
return 0;
}
🚩半缺省参数
🌰举个栗子👇
//半缺省参数
void Func(int a, int b = 10, int c = 20)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
}
int main()
{
Func(1);
Func(1,2);
Func(1, 2, 3);
return 0;
}
❌下面这种情形是不允许的👇
void Func(int a = 10, int b, int c = 20)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl << endl;
}
🚩缺省参数实用案例
假如要实现一个栈的结构,我们不知道要入栈插入多少个数据,所以不知道初始化开辟多少内存空间合适,给多了浪费,给少了不够用,这时我们就可以使用缺省参数
🌰请看演示代码👇
Stack.h
#include<stdlib.h>
namespace N1
{
typedef struct Stack
{
int* a;
int top;
int capacity;
}ST;
//不允许声明和定义同时给缺省参数
//声明给,定义不给
void StackInit(ST* ps, int N = 4)
{
ps->a = (int*)malloc(sizeof(int) * N);
ps->top = 0;
ps->capacity = 0;
}
Test.cpp
#include "Stack.h"
int main()
{
N1::ST st1;
StackInit(&st1, 10);//知道10个
for (size_t i = 0; i < 10; i++)
{
StackPush(&st1, i);
}
N1::ST st2;
StackInit(&st2, 100);//知道100个
for (size_t i = 0; i < 100; i++)
{
StackPush(&st2, i);
}
//不知道可能会插入多少个
N1::ST st3;
StackInit(&st3);
return 0;
}
🥮二、函数重载
🍕1、函数重载概念
重载函数之间是通过函数的 形参个数 或 形参类型 或 形参类型顺序 来区分的(只有这三个区分标准),重载函数之间的区分在于形参的差异。
1️⃣参数类型不同
// 1、参数类型不同
int Add(int left, int right)
{
cout << "int Add(int left, int right)" << endl;
return left + right;
}
double Add(double left, double right)
{
cout << "double Add(double left, double right)" << endl;
return left + right;
2️⃣参数个数不同
// 2、参数个数不同
void func1()
{
cout << "func1()" << endl;
}
void func1(int a)
{
cout << "func1(int a)" << endl;
}
3️⃣参数类型顺序不同
// 3、参数类型顺序不同
void func2(int a, char b)
{
cout << "func2(int a,char b)" << endl;
}
void func2(char b, int a)
{
cout << "func2(char b, int a)" << endl;
}
int main()
{
cout << Add(10, 20) << endl;
cout << Add(10.1, 20.2) << endl;
cout << endl;
func1();
func1(10);
cout << endl;
func2(10, 'a');
func2('a', 10);
return 0;
}
🚨注意以下两种情况不构成函数重载:
namespace M1
{
void func(int x)
{};
}
namespace M2
{
void func(int x)
{};
}
不在同一作用域中(函数重载必须在同一作用域中)☝️
namespace M1
{
void func(int x)
{};
}
namespace M1
{
void func(int x)
{};
}
自动合并了☝️
namespace M1
{
void func(int x)
{};
}
namespace M1
{
void func(double x)
{};
}
构成函数重载✅
函数重载和缺省参数没有关系:
void func(int a)
{
cout << "void func(int a)" << endl;
}
void func(int a, int b = 1)
{
cout << "void func(int a, int b)" << endl;
}
int main()
{
func(1, 2);
//调用存在歧义,不知道调用哪个
//func(1);
return 0;
}
🥮三、C++支持函数重载的底层原理–函数名修饰
形成可执行文件要四个阶段:预处➡️编译➡️汇编➡️链接
void func(int i, double d)
{
cout << "void func(int i, double d)" << endl;
}
void func(double d, int i)
{
cout << "void func(double d, int i)" << endl;
}
int main()
{
func(1, 1.1); // call func()
func(1.1, 1); // call func()
return 0;
}
对这段代码编译生成反汇编:
call指令是通过函数的符号表来定位函数的地址的
但是在C语言中,函数符号表中函数的命名规则就是原模原样地照搬源文件中的函数标识名。
😍这期内容有一点点难理解,希望烙铁们能理解消化,有所收获哦!