当前位置 : 主页 > 编程语言 > c++ >

C++引用与传参

来源:互联网 收集:自由互联 发布时间:2021-06-23
# include iostreamusing namespace std;void Swap(int *pa, int *pb) { int t = *pa; *pa = *pb; *pb = t; cout "*pa = " *pa endl; }void Swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; cout "tmp = " tmp endl; }int main(){ int a = 3, b = 5;
# include <iostream>
using namespace std;

void Swap(int *pa, int *pb)  
{
    int t = *pa;
    *pa = *pb;
    *pb = t;
    cout << "*pa = " << *pa << endl;
    
}

void Swap(int &a, int &b) {
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    
    cout << "tmp = " << tmp << endl;  

}

int main()
{
    int a = 3, b = 5;
    
    Swap(&a, &b);   /*执行第一个*/
    Swap(a, b);   /*执行第二个*/
    printf("a = %d""; b = %d\n", a, b);
    cout << "a = " << a << "; b = " << b << endl;
    return 0;
}

输出结果

网友评论