关于C++指针引用基础知识

  1) 将引用作为参数

  

#include<iostream>
using namespace std;

void  Log(int& as, int& bn);  // 将参数设置引用.

int main()
{
    int xs = 100, ys = 620;
    cout << "initall a (xs) for: " << xs << " and (ys) for: " 
                   << ys << endl;

    Log(xs, ys);
    cout << "uppdated (xs) for: " << xs 
         << " and (ys) for: " << ys <<endl;

    return 0;   
}

void Log(int& ap, int& bp)
{
    int temp = ap;        // 传入 ap 的地址.
      ap = bp;     // bp 地址传入 ap. 
    bp = temp;    // 即temp = ap = bp.
}

 

  2)引用作为返回值

 

#include<iostream>
using namespace std;
double vlas_array[] = { 10.2, 32,3, 40,2 ,81,9, 30.0 };

 double& Pointer_cite(int i);   // double类型函数定义引用.

int main()
{
// 引用前:
cout << "No have by cite a array for: " << endl; for (int k = 0; k < 5; k++) { cout << "vlas_array[" << k << "]: " << vlas_array[k] << endl;; } cout << "\n\n\n"; // 引用后:
Pointer_cite(1) = 53.6; // 修改索引内容值. Pointer_cite(4) = 100.2;   cout << "No have by cite a array for: " << endl; for (int k = 0; k < 5; k++) { cout << "vlas_array[" << k << "]: " << vlas_array[k] << endl; } return 0; } double& Pointer_cite(int i) { double& vaf = vlas_array[i]; // 同理引用数组索引内容. return vaf;       }

 

posted @ 2021-03-16 20:42  #无止境  阅读(27)  评论(0)    收藏  举报