标题:关于reference variable
只看楼主
菜鸟一只
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-2-6
 问题点数:0 回复次数:1 
关于reference variable
关于passing by value 和 passing by reference 我始终不能理解
请高手指点
 参照下边的程序
 #include <iostream>
using namespace std;
void swapr(int & a, int & b);  
void swapp(int * p, int * q);   
void swapv(int a, int b);      
int main()
{
    int wallet1 = 300;
    int wallet2 = 350;

    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << "\ n";

    cout << "Using references to swap contents:\ n";
    swapr(wallet1, wallet2);   
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << "\ n";

    cout << "Using pointers to swap contents:\ n";
    swapp(&wallet1, &wallet2);
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << "\ n";

    cout << "Trying to use passing by value:\ n";
    swapv(wallet1, wallet2);   
    cout << "wallet1 = $" << wallet1;
    cout << " wallet2 = $" << wallet2 << "\ n";
    return 0;
}

void swapr(int & a, int & b)    {
    int temp;

    temp = a;      
    a = b;
    b = temp;
}

void swapp(int * p, int * q)   
{
    int temp;

    temp = *p;      
    *p = *q;
    *q = temp;
}

void swapv(int a, int b)        
{
    int temp;
    temp = a;      
    a = b;
    b = temp;
}
搜索更多相关主题的帖子: reference int variable void 
2005-02-10 22:44
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
得分:0 
我想你需要时间去真正理解 reference, 或者说真正领悟什么是reference.  为此大概需要2年的时间.  相信我,到时候你一定会理解.

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-11 10:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-12126-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052653 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved