C++中的函數用法同C語言的區別不大,出了參數傳遞能夠用引用以外。ios
參數傳遞能夠是:傳值,傳指針,傳引用。api
返回值:各類數據類型,指針,引用。app
這裏講一個對指針的引用。函數
舉一個交換兩個整形數的例子來講明:spa
源程序以下:指針
#include <iostream>
using namespace std;
void iswapint(int a,int b) //第一個輸出結果,變量的值和地址沒有任何變化,傳遞的是變量的一個拷貝。
{
int t=a;
a=b;
b=t;
}
void iswapref(int &a,int &b)//使用引用,直接對變量操做,地址沒有變化,變量的數值變化了。
{
int t=a;
a=b;
b=t;
}
void iswapptr1(int *a,int *b)//使用指針傳遞,裏面的方法,*a等,其實這個叫作間接引用指針。
{
int t=*a;
*a=*b;
*b=t;
}
void iswapptr2(int *a,int *b)//指針傳遞,同第一個同樣,傳遞的是指針的拷貝,對原來的變量和地址都沒有改變。
{
int *t=a;
a=b;
b=t;
}
void iswaptrref1(int *&a,int *&b)//變量的值沒有改變,可是改變了變量的地址,所以經過地址找變量的數值就不同了
//這個至關於有兩個房子A和B在廣州和香港,裏面對應的是一個男人(Y)和一個女人(X),把廣州和香港對調,地址變了
//可是A房子仍是Y男,B房子仍是X女,若是經過地址找則香港是Y男了,而廣州是X女了。
{
int *t=a;
a=b;
b=t;
}
void iswaptrref2(int *&a,int *&b)//這個結果是由於上面的函數緣由形成的,其實這個跟void iswapptr1(int *a,int *b)//使用指針傳遞,裏面的方法,*a等,其實這個叫作間接引用指針。
{
int t=*a;
*a=*b;
*b=t;
}
int main()
{
int x=5,y=9;
int *ptx=&x;
int *pty=&y;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
iswapint(x,y);
//iswapref(x,y);
//iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
iswapref(x,y);
//iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
//iswapref(x,y);
iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
//iswapref(x,y);
//iswapptr1(ptx,pty);
iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
//iswapref(x,y);
//iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
iswaptrref1(ptx,pty);
//iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
cout<<"Original value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
//iswapint(x,y);
//iswapref(x,y);
//iswapptr1(ptx,pty);
//iswapptr2(ptx,pty);
//iswaptrref1(ptx,pty);
iswaptrref2(ptx,pty);
cout<<endl;
cout<<"Changed value: x= "<<x<<" y= "<<y<<endl;
cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl;
cout<<"Address: x="<<ptx<<" y= "<<pty<<endl;
return 0;
}
輸出結果:io
Function is: swap(int ,int)
Original value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Function is: swap(int &,int &)
Original value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 9 y= 5
*ptx= 9 *pty= 5
Address: x=0xbfd9b448 y= 0xbfd9b444
Function is: swap(int *,int *)
Original value: x= 9 y= 5
*ptx= 9 *pty= 5
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Function is: swap(int *,int *)
Original value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Function is: swap(int *&,int *&)
Original value: x= 5 y= 9
*ptx= 5 *pty= 9
Address: x=0xbfd9b448 y= 0xbfd9b444
Changed value: x= 5 y= 9
*ptx= 9 *pty= 5
Address: x=0xbfd9b444 y= 0xbfd9b448
Function is: swap(int *&,int *&)
Original value: x= 5 y= 9
*ptx= 9 *pty= 5
Address: x=0xbfd9b444 y= 0xbfd9b448
Changed value: x= 9 y= 5
*ptx= 5 *pty= 9
Address: x=0xbfd9b444 y= 0xbfd9b448stream