将引用作为函数参数

1:void swapr (int a,int b)
{
   int temp;
   temp=a;
   a=b;
   b=temp
   }


    这里的temp是已经定义的变量,也就是我们说的引用,在上面的函数中,首先定义了一个函数,上面先定义,再按值传递,但是c++中是不可以的。只有指针传递,引用传递。
指针传递
viod swapv(int *p,int*q)
{
int temp;
temp=*P;
*p=*q;
*q=temp;
}
引用传递
void swapr(int &a,int&b)
{
int temp;
temp=a;
a=b;
b=temp;
}
二:引用的属性和特别之处
include
double cube(double a);
double refcube(double &ra);
int main()
{
  using namespace std;
  double x=3.0;
  cout<

临时变量,引用参数和const
1:实参类型正确,但不是左值,
2实参类型不正确,但可以转换成为正确的类型。
double refcube(const double &ra)
{
return rarara;
}
左值就是变量,数组元素,结构成员,引用和解除引用用的指针。
常规变量和const变量都是左值,因为可以通过地址访问它们。
尽可能使用const
1:使用const可以避免无意中修改数据的编程错误
2:使用const使函数能够处理const和非const实参,否则只能接受非const数据
3:使用const引用使函数声明为const.
double &&rref=std::sqrt(36.00);
double j=15.0;
double &&jref=2.0*j+18.5;
std::cout< std::cout< &&叫做右值引用,

你可能感兴趣的:(函数深入)