#include <iostream>

/* firstref.cpp -- defining and using a reference */

 

int main( )
{
using namespace std;
int rats=101;
int &rodents=rats; //主要是當做函數的型式參數

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

rodents++;

cout<<"rats="<<rats; //102
cout<<",rodents="<<rodents<<endl; //102
cout<<"rats address="<<&rats; //0x23fed8
cout<<", rodents address ="<<&rodents<<endl; //0x23fed8


return 0;
}

 

 

顯示結果在註解
可以發現
&rats=&rodents

在同一個位置

 

其中int &rodents=rats;

舉例來說呢:
就是我的名字是布拉怡,
然後再取一個暱稱叫做Niki一樣
就變成:
int &Niki=布拉怡;
都在同一個家,同一個人,
所以我的年齡一樣,住址一樣

arrow
arrow
    全站熱搜

    布拉怡 發表在 痞客邦 留言(0) 人氣()