基础知识-通过引用来传递函数参数(直接访问函数参数)

来源:互联网 发布:mac地址计算pin码软件 编辑:程序博客网 时间:2024/05/17 07:50

实例:

#include "stdafx.h"
#include "iostream.h"
void sum(float &ra ,float &rb)//别名直接引用方式
{
  float x;
   x=ra;
   ra=rb;
   rb=x;
}

int main()
{
 float a, b;
 a=2;
 b=3;
 cout<<"转换前a的值为:"<<a<<endl;
 cout<<"转换前b的值为:"<<b<<endl;
 sum(a,b);
 cout<<"转换后a的值为:"<<a<<endl;
 cout<<"转换后b的值为:"<<b<<endl;
 return 0;
}

指针是间接访问,引用时直接访问。