C++学习笔记-6-函数

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

相关文章
相关标签/搜索