zoukankan      html  css  js  c++  java
  • 引用类型 C++Primer 读书笔记

          引用 :
     
          一 、 引用只是它绑定对象的别名

        int i = 10;
        
    int &refVal_i = i;
        i 
    = 5;
        cout 
    << refVal_i << endl;   // 5;
          reVal_i 和 i 都是同一块内存的名称。

          二 、 const类型的引用

        int i = 10;
        
    int &refVal_i = i; //必须要变量赋值
        int &refVal_j= 10//error! 不能直接赋值
        const int &refVal_j = 10//ok! 
          注意const类型引用的区别,具体的还要在日后体会!

          三 、 引用一般用于函数中形参

          我们做一个函数,用于交换传进来2个int的值:
    Code

          而实际i,j的值并没有改变,所以这时就需要引用类型了。

    void referencefun(int &the_i,int &the_j)
    {
        
    int temp = the_i;
        the_i 
    = the_j;
        the_j 
    = temp;
    }
          
          此时输出为 20 ,10 ,说明 i , j更换成功!
  • 相关阅读:
    es index template
    什么是元类
    Normal Data Structure Tricks
    Python 学习笔记
    点分治
    人类智慧贪心
    「JOI 2021 Final」地牢 3
    【美团杯2020】魔塔
    CF917D 的垃圾做法
    【ULR #2】Picks loves segment tree IX
  • 原文地址:https://www.cnblogs.com/sevenyuan/p/1557200.html
Copyright © 2011-2022 走看看