C++ ——基础进阶

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;
复制代码
}
相关推荐
周Echo周1 小时前
6、STL中list的使用方法
数据结构·c++·windows·后端·算法·链表·list
南枝异客1 小时前
HTML&CSS绘制三角形
开发语言·前端·css·html
longerxin20201 小时前
使用curl随机间隔访问URL-使用curl每秒访问一次URL-nginx
c语言·开发语言·bash
Source.Liu2 小时前
【CXX】6.5 Box<T> — rust::Box<T>
c++·rust·cxx
愚戏师3 小时前
Python :数据模型
开发语言·python
慕瑶琴3 小时前
SQL语言的编译原理
开发语言·后端·golang
dorabighead7 小时前
重构版:JavaScript 的 new 操作符——从“黑箱仪式”到“亲手造物”的认知跃迁
开发语言·javascript·重构
Humbunklung7 小时前
C#中通过Response.Headers设置自定义参数
开发语言·c#
Trouvaille ~7 小时前
【Java篇】一法不变,万象归一:方法封装与递归的思想之道
java·开发语言·面向对象·javase·递归·方法·基础入门
wtrees_松阳7 小时前
【编程向导】-JavaScript-基础语法-类型检测
开发语言·javascript·原型模式