引用的使用场景
- 引用作为函数参数
#include <iostream>
using namespace std;
//1.引用作为函数参数
void func(int &a, int &b)
{
int sum = a + b;
cout << "sum=" << sum << endl;
}
void test01()
{
int a = 10;
int b = 20;
func(a, b);
}
int main()
{
test01();
return 0;
}
- 引用作为函数的返回值
#include <iostream>
using namespace std;
//2.引用作为函数的返回值
int& func2()
{
//注意1:不要返回局部变量的引用
int b = 10;
int &p = b;
return p;
}
int &func3()
{
static int b = 10;
return b;
}
void test02()
{
int &q = func2();
q = 100;
cout << q << endl;
func2() = 200;
cout << q << endl;
---------上面的代码是错误,只是编译器没有检测出来
cout << "func2="<<func2() << endl;
//注意2:如果要函数当左值,那么该函数必须返回引用
func3() = 100;
cout << "func3()=" << func3() << endl;
}
int main()
{
test02();
return 0;
}
常量引用
int &ref=10;//err
const int &ref2=10;//ok
原理是:int tmp=10;const int &ref=tmp;
内联函数
- 宏函数的缺陷
#include <iostream>
using namespace std;
#define ADD(x,y) x+y
//在普通函数前面加上inline是向编译器申请成为内联函数
//注意:加inline可能成为内联函数,可能不成为内联函数
inline int Add(int x, int y)
{
return x + y;
}
void test()
{
//10+20*2
int ref = ADD(10, 20) * 2;
cout << "ref=" << ref << endl;
int ref2 = Add(10, 20) * 2;
cout << "ref2=" << ref2 << endl;
}
#define COMAPD(x,y) ((x)<(y)?(x):(y))
inline int func(int x, int y)
{
return x < y ? x : y;
}
void test02()
{
int a = 1;
int b = 3;
// ((++a)<(b)?(++a):(b))
//cout << "COMAPD(x,y)=" << COMAPD(++a, b) << endl;//3
cout << "func=" << func(++a, b) << endl;//2
}
int main()
{
test();
test02();
return 0;
}
- 什么情况不会成为内联函数
- 存在过多的条件判断语句
- 函数体过大
- 对函数进行取址操作
- 存在任何形式的循环语句
- 内联函数的好处
- 有宏函数的效率,没有宏函数的缺点
- 类的成员函数默认加上inline
- 在普通函数前面加上inline是申请成为内联函数
函数的默认参数
- 函数的默认参数就是给函数的形参赋值
#include <iostream>
using namespace std;
int myFunc(int a, int b = 0) // int b =0 ;这就是函数的默认参数,不一定是0
{
return a+b;
}
void test01()
{
// 函数的默认参数的作用
// 当函数内常要用到形参某个值,但偶尔要使用其他值
// 增加函数的灵活性
cout << myFunc(10,20) << endl;
cout << myFunc(10) << endl;
}
int main()
{
test();
return 0;
}
- 函数的默认参数的注意事项
#include <iostream>
using namespace std;
// 注意1:函数默认参数后面的参数必须都是默认参数
int myFunc2(int a, int b = 0, int c = 2,int d = 3)
{
return a + b + c + d;
}
// 注意2:函数的声明和实现不能同时有函数的默认参数
void myFunc3(int a,int b);
void myFunc3(int a, int b = 0)
{
}
int main()
{
myFunc2();
return 0;
}
函数的占位参数
#include <iostream>
using namespace std;
// 占位参数原形
void func1(int)
{
}
// 函数的占位参数,占位参数在后面运算符重载时区分前加加或后加加
void func(int a,int = 10) // 占位参数也有默认值
{
}
void test()
{
func(10);
}
int mian()
{
return 0;
}
函数的默认参数和占位参数的混搭
#include <iostream>
using namespace std;
// 占位参数和默认参数混搭
void
void func(int = 10,int a = 20)
{
}
void test()
{
func();
func(10);
func(10,30);
}
int main ()
{
return 0;
}
函数重载
-
函数重载是:允许函数名相同,这种现象叫函数重载
-
函数重载的作用:是为了方便使用函数名
-
函数重载的条件:同一个作用域,参数的个数不同,参数的顺序不同,参数的类型不同
//参数的个数不同
#include <iostream>
using namespace std;
void func()
{
cout << "func()" << endl;
}
void func(int a)
{
cout << "func(int a)" << endl;
}
//参数的类型不同
void func(char c)
{
cout << "func(char c)" << endl;
}
//参数的顺序不同
void func(int a, double b)
{
cout << "func(int a, double b)" << endl;
}
void func(double b, int a)
{
cout << "func(double b, int a)" << endl;
}
void test()
{
int a = 10;
double b = 3.14;
func();
//func(b);// err double转换不了成为int或char
func(a, b);
func(b, a);
char c = 'c';
func(c);//char转换int成功,调用int参数的函数
}
int main()
{
test();
return 0;
}
- 调用重载函数的注意
严格的类型匹配,如果类型不匹配,那么尝试转换,转换成功就调用,失败就报错(结合上面代码看)
#include <iostream>
using namespace std;
void test()
{
int a = 10;
double b = 3.14;
func();
//func(b);// err double转换不了成为int或char
func(a, b);
func(b, a);
char c = 'c';
func(c);//char转换int成功,调用int参数的函数
}
- 函数重载和函数的默认参数一起使用,需要注意二义性问题
#include <iostream>
using namespace std;
//函数重载和函的默认参数一起使用
void myfunc(int a, int b = 0)
{
cout << "myfunc(int a, int b = 0)" << endl;
}
void myfunc(int a)
{
cout << "myfunc(int a)" << endl;
}
void test()
{
//myfunc(10); err,二义性问题,不知道调用哪个函数
}
int main()
{
test();
return 0;
}
- 函数的返回值不作为函数重载的条件
编译器是通过调用函数时,传入的参数来判断调用重载的那个函数,我们调用函数时不需要写返回值,所有返回值不能成为函数的重载条件
函数重载的原理
- 函数重载的原理是在汇编时,给各个函数取别名,C语言不能重载的原因是没有取别名
- 生成汇编文件:gcc/g++ -S test.c -o test.s
- windows在cmd中查看内容:type 文件名
图片解析
C++调用C语言的函数
C++的函数在汇编时,会给函数取别名,C语言的不会,这时,如果C++来调用C语言的函数,C++会去找取了别名的函数,但是C语言没有取别名,这时会出错
test.h文件中
//这是告诉C++编译器,找下面的函数,要以C语言的方式去寻找
#ifdef __cplusplus
extern "C"
{
#endif
void func();//C语言的函数声明
#ifdef __cplusplus
}
#endif
test.c文件
#include <stdio.h>
#include "test.h"
void func() {
printf("func()函数被调用\n");
}
test.cpp文件
#include <iostream>
#include "test.h";
using namespace std;
int main()
{
func();
return 0;
}
注意:如果func()函数写成如下这样会报错
test.h文件
void func();//C语言的函数声明
持续更新中,欢迎大家一起讨论和参考 |