對引用和指針使用以及函數返回引用和指針類型的理解

#include "stdafx.h"
#include <iostream>
using namespace std;
int globalJ =999;
 
//返回值
int test1()
{
    int j =1;
    cout<<"in test1(),[return value]  the varaible j's address :"<<&j<<endl;
 
    return j;
}
 
//使用局部變量,返回引用
int& test2()
{
 int j =998;
 cout<<"in test2(),[use field variable and return reference]  the local varaible J's address :"<<&j<<endl;
 cout<<"in test2(),[use field variable and return reference]  the local varaible J's value :"<<j<<endl;
 
  return j;
}
 
//使用全局變量,返回引用
int& test3()
{
   
    cout<<"in test3(),[use global variable and return reference] the varaible globalJ's address :"<<&globalJ<<endl;
   
    return globalJ;
}
 
 
//返回指針
int* test4()
{
    int j =998;
    cout<<"in test4(),[use field variable and return pointer]  the local varaible J's address :"<<&j<<endl;
 cout<<"in test4(),[use field variable and return pointer]  the local varaible J's value :"<<j<<endl;
 
 
  return &j; 
}
//返回指針
int* test5()
{
    cout<<"in test5()[use global variable and return pointer] , the varaible globalJ's address :"<<&globalJ<<endl;
    return &globalJ; 
}
 
 
int main(int argc, char* argv[])
{
    printf("Hello functions!/n");
   
    int testresultvalue =0;
    testresultvalue = test1();
    cout<<"testResultValue address :"<<&testresultvalue <<endl;
    cout<<"testResultValue value:"<<testresultvalue<<endl;
    cout<<"slit line----------------------------------"<<endl;
 
 
    int & testResultReference  = test2();
    cout<<"testResultReference address :"<<&testResultReference <<endl;
    cout<<"testResultReference value:"<<testResultReference<<endl;
    cout<<"slit line----------------------------------"<<endl;
 
    testResultReference = test3();
    cout<<"testResultReference address :"<<&testResultReference <<endl;
    cout<<"testResultReference value:"<<testResultReference<<endl;
    testResultReference = 4;
    cout<<"reset to 4"<<endl;
    cout<<"testResultReference address :"<<&testResultReference <<endl;
    cout<<"testResultReference value:"<<testResultReference<<endl;
   
    cout<<"slit line----------------------------------"<<endl;
 
    int & testResultReference2 = test3();
    cout<<"testResultReference2 address :"<<&testResultReference2 <<endl;
    cout<<"testResultReference2 value:"<<testResultReference2<<endl;
    cout<<"slit line----------------------------------"<<endl;
 
    int* testResultPtr;
    testResultPtr = test4();
    cout<<"testResult address :"<<testResultPtr <<endl;
    cout<<"testResult value:"<<*testResultPtr<<endl;
    cout<<"slit line----------------------------------"<<endl;
   
    testResultPtr = test5();
    cout<<"testResult address :"<<testResultPtr <<endl;
    cout<<"testResult value:"<<*testResultPtr<<endl;
    cout<<"slit line----------------------------------"<<endl;

    int temp;
    cin>>temp;
 
    return 0;
}

 

然後我們來分析結果,

test1 是返回值,沒有什麼好說的。

test2

image

本地變量的J的值是998,引用後得到的值是1245056. 因爲在function作用域以外這塊內存的東西就被釋放掉了,所以值就不一致了。實際上,應該用全局變量(或類變量)。

注意:J的內存地址和引用變量的地址是一樣的。

在編譯中會報警告信息:

warning C4172: returning address of local variable or temporary

test3

image

globalJ全局變量的地址是00478DC0,但引用變量testresultreference的內存地址仍然是0012FF14。

結論:引用變量一次賦值後即爲只讀,即使再次賦值,其引用對象不變。

image

根據上面的情況我們新定義一個引用變量testresultreference2, globalJ的地址和引用變量地址是一致的。

test4

image

這個例子跟引用有點相似。雖然指針指向的仍是本地變量的地址,但值發生了改變。

結論:指針指向局部變量,一旦超出變量域,值就有問題了。

test5

image

結論:請注意指針變量可以多次賦值,這樣就改變了指向的對象。這點跟引用對象的使用方式不同。

發佈了159 篇原創文章 · 獲贊 16 · 訪問量 102萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章