引用(一)

/*
~~与指针一样不太好理解~~
一、创建引用变量
1.引用变量的创建:
int rats;
int & rodents = rats;
int &指的是指向int的引用。
上述引用声明允许将rats和rodents互换,它们指向相同的值和内存单元。
2.注意:
必须在声明引用时将其初始化,而不能像指针那样,先声明,在赋值。
int rat;
int & rodent;
rodent = rat;
3.引用更接近const指针,必须在创建时进行初始化,一旦与某个变量关联起来,就将一直效忠于他。
int & rodents = rats;
实际上是下述代码的伪装表示:
int * const pr = &rats;
其中,引用rodents扮演的角色与表达式*pr相同。
下面的程序演示了试图将rats变量的引用改为bunnies变量的引用时,将发生的情况。
*/
/*
下面看个小程序:
#include <iostream>


using namespace std;


int main()
{
    int rats = 101;
    int & rodents = rats;


    cout << "rats = " << rats;
    cout << ",rodents = " << rodents << endl;


    cout << "rats address = "  << &rats;
    cout << ", rodents address = " << &rodents << endl;


    int bunnies = 50;
    rodents = bunnies;
    cout << "bunnies = " << bunnies;
    cout << ", rats = " << rats;
    cout << ", rodents = " << rodents << endl;


    cout << "bunnies address = " << &bunnies;
    cout << ",rodents address = " << &rodents << endl;
    return 0;
}


rats = 101,rodents = 101
rats address = 0x28fef8, rodents address = 0x28fef8
bunnies = 50, rats = 50, rodents = 50
bunnies address = 0x28fef4,rodents address = 0x28fef8


Process returned 0 (0x0)   execution time : 0.328 s
Press any key to continue.
*/
/*rodents = bunnies;就相当于 rats = bunnies;
简而言之,可以通过初始化声明来设置引用,但不能通过赋值来设置。


4.假如我们这样做:
int rats = 101;
int * pt = &rats;
int & rodents = *pt;
int bunnies = 50;
pt = bunnies;
将rodents初始化为*pt使得rodents指向rats。接下来pt改为指向bunnies,并不能改变这样
的事实,即rodents引用的是rats.
二、将引用作为函数参数
引用经常被用作函数参数,使得函数中的变量名成为调用程序中的变量的别名。这种传递参数的方法
称为按引用传递。按引用传递允许被调用的函数能够访问调用函数中的变量。
交换两个变量的值的程序是一个很好的例子,我们可以使用指针,引用做个比较。
程序如下:*/
/*
#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 << endl;


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


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


    cout << "Trying to use passing by value: \n";
    swapv(wallet1,wallet2);
    cout << "wallet1 = $ " << wallet1;
    cout << " wallet2 = $" << wallet2 << endl;
    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;
}
*/
///我又结合之前学过的内联函数把程序改了改:
/*#include<iostream>
using namespace std;
void swapr(int & a, int & b);
void swapp(int * p, int * q);
void swapv(int a, int b);
inline int output(int a, int b)
{
    cout << "wallet1 = $" << a;
    cout << " wallet2 = $" << b << endl;
    return 0;
}
int main()
{
    int wallet1 = 300;
    int wallet2 = 350;


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


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


    cout << "Trying to use passing by value: \n";
    swapv(wallet1,wallet2);
    output(wallet1,wallet2);
    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;
}


wallet1 = $300 wallet2 = $350
Using references to swap contents:
wallet1 = $350 wallet2 = $300
Using pointers to swap contents:
wallet1 = $300 wallet2 = $350
Trying to use passing by value:
wallet1 = $300 wallet2 = $350


Process returned 0 (0x0)   execution time : 0.250 s
Press any key to continue.

*/

唉,学习C++是一天中最幸福的事,学习算法是一天中最痛苦的事。

我马上就要去找痛苦玩耍了。
















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值