1、引用
概念:相当于给变量取个别名,通过使用&在变量定义时定义
1.1 性质
(1)成为一个变量的引用后,就不能成为其他变量的引用
int a=1;
int& a_cite=a;
int b=90;
a_cite=b; //相当于把b的值给了a_cite
cout<<a_cite<<endl; //90
cout<<a<<endl; //90
cout<<b<<endl; //90
a++;
cout<<a_cite<<endl; //91
cout<<a<<endl; //91
cout<<b<<endl; //90
// int&a_cite=b; //错误
(2)引用必须初始化,并且不可以为NULL
int c=0;
// int&c_cite; //错误,引用必须初始化
// int&c_cite=NULL; //错误,不能初始化为NULL
(3)引用可以是纯数字的引用,当作为纯数字的引用时,需要加const修饰,代表其引用的数值不可修改
const int&cite=55;
cout<<cite<<endl;
// cite++; //报错,纯数字的引用不能修改
(4)变量的引用如果使用const修饰,此时该引用叫常引用,表示不能通过引用修改变量的值,但可以通过原变量修改变量的值
int d=1;
const int&d_cite=d; //常引用
// d_cite=90; //错误,常引用不能修改变量的值
d=100;
cout<<d_cite<<endl; //100
cout<<d<<endl; //100
(5)可以给指针变量建立引用,相当于给指针变量取了别名,该引用指向原变量
int f=7;
int*p=&f;
int*&p_cite=p; //整型指针引用
cout<<p<<endl; //0000003D2D13F67C
cout<<*p<<endl; //7
cout<<p_cite<<endl; //0000003D2D13F67C
cout<<*p_cite<<endl; //7
*p_cite=1;
cout<<f<<endl; //1
cout<<*p<<endl; //1
cout<<*p_cite<<endl; //1
(6)可以是指针指向引用的地址,相当于指向了变量的地址
int e=90;
int&e_cite=e;
// int*ptr=&e; //方法一
int*ptr=&e_cite; //方法二
cout<<*ptr<<endl; //90
*ptr=100;
cout<<e<<endl; //100
cout<<e_cite<<endl; //100
1.2 函数参数的传递
C++中函数参数的传递有三种:
(1)值传递:只是实参的拷贝,形参改变不会影响到实参
#include <iostream> //iostream:输入输出流文件 ,相当于stdint.h
using namespace std; //命名空间
void add_number1(int number);
int main() //入口函数
{
int number=1;
add_number1(number);
cout<<number<<endl; //1
return 0;
}
void add_number1(int number){
number++;
cout<<number<<endl; //2
}
(2)引用传递:不会产生拷贝副本,相当于操控变量本身,形参改变会影响到实参
#include <iostream> //iostream:输入输出流文件 ,相当于stdint.h
using namespace std; //命名空间
void add_number2(int& number);
int main() //入口函数
{
//引用传递:是把变量的引用传递函数,函数内部对参数的改变,函数外部的变量也会受到影响
int number=1;
add_number2(number);
cout<<number<<endl; //2
return 0;
}
void add_number2(int& number){
number++;
cout<<number<<endl; //2
}
(3)指针传递:是把变量的地址传递给函数,函数内部对变量的修改也会影响带函数外部原来的变量
建议:
(1)如果函数体内对参数的改变,在函数体外需要受到影响,推荐选择引用传递,不推荐指针传递
#include <iostream> //iostream:输入输出流文件 ,相当于stdint.h
using namespace std; //命名空间
void change_number1(int&c1,int&c2);
int main() //入口函数
{
int num1=7,num2=9;
change_number1(num1,num2);
cout<<"num1="<<num1<<",num2="<<num2<<endl; //num1=9,num2=7
return 0;
}
void change_number1(int&c1,int&c2){
int num=0;
num=c1;
c1=c2;
c2=num;
}
(2)如果函数体内对参数的改变,在函数体外不需要受到影响,推荐使用引用传递,加const修饰
#include <iostream> //iostream:输入输出流文件 ,相当于stdint.h
using namespace std; //命名空间
void change_number2(const int& num);
int main() //入口函数
{
int num1=7,num2=9;
change_number2(num1);
cout<<num1<<endl; //9
return 0;
}
void change_number2(const int& num){
cout<<num*100<<endl; //900
}
2、内联函数inline
定义:使用inline关键字修饰的函数,取代了C中宏函数
对比:普通函数需要经历函数调用,函数调用比较消耗性能;内联函数与宏函数相同,是在预处理阶段,进行函数体的展开,不需要经历函数调用
总结:内联函数是一个空间换时间的方法,缺点:会增大可执行文件的体积;优点:不需要经历函数调用的过程,提高了效率
内联函数的使用条件:
(1)函数体逻辑代码不能太复杂,不能包含for、while、switch等符合语句
(2)函数代码少于5行
(3)函数调用频繁
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
inline void func();
int main(){
func();
return 0;
}
inline void func(){
cout<<"gbsrhwrw"<<endl;
}
3、函数参数的默认值
C++中允许给函数参数添加默认值,调用函数时,如果不传递参数则使用默认值;如果传递参数则覆盖默认值。默认值可以提高程序的灵活性
注意事项:
(1)函数定义和声明分离时,推荐函数参数的默认值设置在声明处
(2)函数声明和定义不分离,函数参数的默认值设置在定义处
(3)当函数有多个参数时,函数参数的默认值遵循"向右(向后)原则",即一个参数有默认值,其后的参数都要有默认值
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void fn1(int a,int b=1);
int main(){
fn1(100,200); //300
fn1(100); //101
return 0;
}
void fn1(int a,int b){
cout<<"a+b="<<a+b<<endl;
}
4、函数的重载
C++中允许定义同名函数来处理相同的业务逻辑
注意:
(1)重载函数调用时,可以根据参数的类型不同、参数的个数不同进行区分,不能通过返回值类型不同进行区分
(2)函数重载不要和函数默认值一起使用,容易产生错误
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void show();
void show(int a);
void show(string str);
void show(int a,int b);
int main(){
show();
show(89);
show("hello");
show(12,34);
return 0;
}
void show(){
cout << "哈哈哈" << endl;
}
void show(int a){
cout << a << endl;
}
void show(string str){
cout << str << endl;
}
void show(int a,int b){
cout << a+b << endl;
}